views:

154

answers:

3

I have a set of images in a folder call 'uploads/', all the files are in this form 5f0f905706.jpg, 15758df106.jpg, ...

I want to rename them as is, 001.jpg, 002.jpg, 003.jpg ...

how i code this? thanks

+4  A: 

glob(), foreach(), str_pad(), rename()

Col. Shrapnel
+1  A: 

like

 foreach(glob("uploads/*jpg") as $n => $file) {
    $new = dirname($file) . '/' . sprintf("%04d", $n + 1) . '.jpg';
    rename($file, $new);
 }
stereofrog
+1  A: 
$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;
}
ghostdog74