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

Rename a large number of pictures

WBOY
Release: 2016-07-06 13:53:26
Original
1186 people have browsed it

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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>
Copy after login

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>
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template