2

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

robertdd
  • 325
  • 1
  • 8
  • 22
  • It's uncanny how identical this question is to one I asked a while back: http://stackoverflow.com/questions/880467/renaming-a-set-of-files-to-001-002-on-linux (though technically not a dupe). – DisgruntledGoat Mar 25 '10 at 13:09

3 Answers3

3

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

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
2
$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
  • 327,991
  • 56
  • 259
  • 343
1

like

 foreach(glob("uploads/*jpg") as $n => $file) {
    $new = dirname($file) . '/' . sprintf("%04d", $n + 1) . '.jpg';
    rename($file, $new);
 }
user187291
  • 53,363
  • 19
  • 95
  • 127