Home >Backend Development >PHP Tutorial >Rename a large number of pictures

Rename a large number of pictures

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-06 13:53:261216browse

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

Reply content:

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn