将一组文件重命名为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, ...

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

how i code this? thanks

$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;
}

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

like

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