There are 1000 images in the IMG folder, how to change the 1000 images to the name 1 to 1000???
$arr = glob("img/*.jpg");var_dump($arr);$i = 1;foreach($arr as $file) { // 获取图片后缀名 $ext = pathinfo($file,PATHINFO_EXTENSION); $name = $i++ . "." . $ext; // 重命名 rename($file, $name);}
Solved the
Reply content:
There are 1000 images in the IMG folder, how to change the 1000 images to the name 1 to 1000???
$arr = glob("img/*.jpg");var_dump($arr);$i = 1;foreach($arr as $file) { // 获取图片后缀名 $ext = pathinfo($file,PATHINFO_EXTENSION); $name = $i++ . "." . $ext; // 重命名 rename($file, $name);}
Solved the
You can use bash, assuming you have this PHP running under Linux, there's bash that can do something like this:shell_exec("i=1;for file in /path/to/img; do mv $file $i; i=$i+1; done")
Of course, PHP actually has a function called Rename:rename PHP manual, you can:
$arr = scandir("/path/to/img");$i = 1;foreach($arr as $file) { rename($file, $i++);}
Notice the PHP version.
// 获取图片列表$arr = scandir("/path/to/img");$i = 1;foreach($arr as $file) { // 获取图片后缀名 $ext = pathinfo($file,PATHINFO_EXTENSION); $name = $i++ . "." . $ext; // 重命名 rename($file, $name);}
Recommend you look at Zhang Xin Xu shortly before the JS general-like web structure can use JS to do what
The above is useful nodejs batch change name