There are 1,000 pictures in the img folder. How do I change the names of these 1,000 pictures from 1 to 1,000? ? ? ?
<code>$arr = glob("img/*.jpg"); var_dump($arr); $i = 1; foreach($arr as $file) { // 获取图片后缀名 $ext = pathinfo($file,PATHINFO_EXTENSION); $name = $i++ . "." . $ext; // 重命名 rename($file, $name); } </code>
Solved
There are 1,000 pictures in the img folder. How do I change the names of these 1,000 pictures from 1 to 1,000? ? ? ?
<code>$arr = glob("img/*.jpg"); var_dump($arr); $i = 1; foreach($arr as $file) { // 获取图片后缀名 $ext = pathinfo($file,PATHINFO_EXTENSION); $name = $i++ . "." . $ext; // 重命名 rename($file, $name); } </code>
Solved
You can use bash. Assuming that your php is running under Linux and you have bash, you can do something similar: shell_exec("i=1;for file in /path/to/img; do mv $file $i; i=$i 1; done")
Of course there is actually a function in php called rename: rename php manual, you can:
<code>$arr = scandir("/path/to/img"); $i = 1; foreach($arr as $file) { rename($file, $i++); } </code>
Just pay attention to the php version.
<code>// 获取图片列表 $arr = scandir("/path/to/img"); $i = 1; foreach($arr as $file) { // 获取图片后缀名 $ext = pathinfo($file,PATHINFO_EXTENSION); $name = $i++ . "." . $ext; // 重命名 rename($file, $name); } </code>
I recommend you take a look at what Zhang Xinxu wrote not long ago. What can you do with js for general web page reconstruction?
The above example uses nodejs to batch change names