Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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
You can’t perform that action at this time.