Skip to content

Instantly share code, notes, and snippets.

@sbrajesh
Created August 17, 2016 10:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbrajesh/fbf9f406918c468ac998200d27e9a6d3 to your computer and use it in GitHub Desktop.
Save sbrajesh/fbf9f406918c468ac998200d27e9a6d3 to your computer and use it in GitHub Desktop.
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