Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Copy png files from the directory recursively to the destination directory
<?php
/** Copy PNG files from the Source Directory( and its child directories) to destination as specified
*
* I use it to move all the png files from the extracted subtle patterns directories to another directory
*
* @param string $path the directory which contains our subdirectories
* @param string $destination_dir the absolute path to directory where files will be moved
*/
function copy_png_files( $path, $destination_dir ) {
if ( ! is_readable( $path ) ) {
return ;
}
echo "\r\n Copying..." . $path;
if ( is_file( $path ) ) {
//copy it to the root only if png
if ( strpos( $path, '.png' ) !== false ) {
$new_file = $destination_dir . DIRECTORY_SEPARATOR . basename( $path );
//do not override
if ( ! file_exists( $new_file ) ) {
copy( $path, $new_file );
}
}
return ;
}
//if we are here, It is a directory
$files = scandir( $path );
foreach ( $files as $file ) {
if ( $file == '.' || $file == '..' ) {
continue;
}
copy_png_files( $path . DIRECTORY_SEPARATOR . $file, $destination_dir );
}
}
//Example
//copy_png_files( '/home/sbrajesh/Desktop/color-scheme/patterns', '/home/sbrajesh/Desktop/patterns' );
/**
* Run as
* php copy-png-files.php
* from the command line
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.