简体   繁体   English

如何使用序列号批量重命名照片文件夹中的文件

[英]How can I bulk rename files in a photo folder with sequential numbers

This was a poorly worded question这是一个措辞不佳的问题

My main goal was to display many images or even a range from a set arbitrarily, renaming the files to sequential numbers seemed like it would make the displaying or iterating through the files easier if they just differed by 1 rather than random strings.我的主要目标是显示许多图像,甚至是任意一组图像,将文件重命名为序列号似乎会使显示或迭代文件更容易,如果它们只是相差 1 而不是随机字符串。

-- anyway I'm going to read... seems glob is according to php manual ... The glob() function searches for all the pathnames matching pattern according to the rules used by the libc glob() function -- 无论如何我要阅读...似乎 glob 是根据 php 手册... glob() 函数根据 libc glob() 函数使用的规则搜索所有路径名匹配模式

The files have random names but they are all .jpg's这些文件的名称是随机的,但它们都是 .jpg 的

As an example, "this name".jpg is replaced with "i+1".jpg例如,“this name”.jpg 替换为“i+1”.jpg

So that I can display the photos lazily using a for loop incrementing the numbers.这样我就可以使用递增数字的 for 循环懒惰地显示照片。 The primary purpose is to display the photos regardless of their file names.主要目的是显示照片而不管它们的文件名。

Basically,基本上,

$count = 1;
foreach (glob('*.jpg') as $filename) {
  @rename($filename, $count.'.jpg');
  $count++;
}

echo 'Done';

All image files must be in the current folder (along with the script).所有图像文件都必须在当前文件夹中(与脚本一起)。

You can add a path manually, if you wish:如果您愿意,可以手动添加路径:

 glob('/path/to/*.jpg')
 rename('path/..'.$file etc. )

You can use php glob and rename functions:您可以使用 php glob重命名函数:

$num = 1;
foreach (glob("/path/to/images/*.jpg") as $filename) {
    $fileNoExtension = basename($filename, ".jpg");
    rename ($filename, "$fileNoExtension{$num}.jpg");
    $num++;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM