Skip to content

Instantly share code, notes, and snippets.

@alex-phillips
Created January 25, 2020 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alex-phillips/19922cd434f71f53eaf07af75355c31e to your computer and use it in GitHub Desktop.
Save alex-phillips/19922cd434f71f53eaf07af75355c31e to your computer and use it in GitHub Desktop.
recursively copy files form one dir to another - case insensitive
<?php
$src = $argv[1];
$dest = $argv[2];
if (!$src || !$dest) {
die("Need a source and destination");
}
$src = rtrim($src, '/') . "/";
$dest = rtrim($dest, '/') . "/";
$files = getDirContents($src, $src);
$existing = getDirContents($dest, $dest);
foreach ($files as $file => $fullFile) {
if (array_key_exists($file, $existing)) {
echo "$fullFile => {$existing[$file]}\n";
rename($fullFile, $existing[$file]);
}
}
function getDirContents($dir, $replace, &$results = array()){
$files = scandir($dir);
foreach($files as $key => $value){
$path = realpath($dir.DIRECTORY_SEPARATOR.$value);
if(!is_dir($path)) {
$results[strtolower(str_replace($replace, '', $path))] = $path;
} else if($value != "." && $value != "..") {
getDirContents($path, $replace, $results);
}
}
return $results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment