Renaming a set of files to 001, 002, … php
I have a set of images in a folder call 'uploads/', all the files are in this form 5f0f905706.jpg, 15758df106.jpg, ...开发者_如何学C
I want to rename them as is, 001.jpg, 002.jpg, 003.jpg ...
how i code this? thanks
glob(), foreach(), str_pad(), rename()
$files = glob("*.jpg");
$num=count($files);
$i=1;
foreach ( $files as $filename) {
$n=str_pad($i, $num ,"0",STR_PAD_LEFT);
$newfile = $n.".jpg";
rename($filename,$newfile);
$i+=1;
}
like
foreach(glob("uploads/*jpg") as $n => $file) {
$new = dirname($file) . '/' . sprintf("%04d", $n + 1) . '.jpg';
rename($file, $new);
}
精彩评论