Skip to content

Instantly share code, notes, and snippets.

@gothedistance
Created February 13, 2014 10:06
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 gothedistance/8972668 to your computer and use it in GitHub Desktop.
Save gothedistance/8972668 to your computer and use it in GitHub Desktop.
<?php
function renameForWelcartImage ( $path ) {
if (is_dir($path) === false) {
echo 'not directory path';
exit();
}
$file = scandir($path);
$count = 0;
foreach($file as $key){
if ('.' == $key || '..' == $key) { continue; }
$result = $path.'/'.$key;
if (is_dir($result)) {
$count = 0;
renameForWelcartImage($result);
} elseif (is_file($result)) {
$path_info = pathinfo($result);
$d_tree = explode('/',$path_info['dirname']);
$fileName = $d_tree[count($d_tree) -1];
$count++;
$rename_fileName = '';
if($count == 1) {
$rename_fileName = $path_info['dirname'].'/'.$fileName.".".$path_info['extension'];
} else {
$suffix = str_pad($count-1, 3, "0", STR_PAD_LEFT);
$rename_fileName = $path_info['dirname'].'/'.$fileName."-".$suffix.".".$path_info['extension'];
}
rename($result,$rename_fileName);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment