批量重命名文件夹中的文件-PHP

批量重命名文件夹中的文件-PHP,php,Php,我在一个文件夹中有1000张图片,所有图片中都有SKU#word。例如 WV1716BNSKU#.zoom.1.jpg WV1716BLSKU#.zoom.3.jpg 我需要做的是读取所有文件名并将其重命名为以下名称 WV1716BN.zoom.1.jpg WV1716BL.zoom.3.jpg 因此,从文件名中删除SKU,在PHP中是否可以进行批量重命名?是的,只需打开目录并创建一个循环来访问所有图像并重命名它们,如: <?php if ($handle = opendir('/p

我在一个文件夹中有1000张图片,所有图片中都有SKU#word。例如

WV1716BNSKU#.zoom.1.jpg
WV1716BLSKU#.zoom.3.jpg
我需要做的是读取所有文件名并将其重命名为以下名称

WV1716BN.zoom.1.jpg
WV1716BL.zoom.3.jpg

因此,从文件名中删除SKU,在PHP中是否可以进行批量重命名?

是的,只需打开目录并创建一个循环来访问所有图像并重命名它们,如:

<?php

if ($handle = opendir('/path/to/files')) {
    while (false !== ($fileName = readdir($handle))) {
        $newName = str_replace("SKU#","",$fileName);
        rename($fileName, $newName);
    }
    closedir($handle);
}
?>

参考资料:


完成此操作的步骤非常简单:

  • 使用
    fopen
    readdir
  • 对于每个文件,将文件名解析为段
  • 将旧文件复制到一个直接称为旧文件的新文件中(出于安全原因)
  • 将根文件重命名为新名称
一个小例子:

if ($handle = opendir('/path/to/images'))
{
    /* Create a new directory for sanity reasons*/
    if(is_directory('/path/to/images/backup'))
    {
         mkdir('/path/to/images/backup');
    }

    /*Iterate the files*/
    while (false !== ($file = readdir($handle)))
    {
          if ($file != "." && $file != "..")
          {
               if(!strstr($file,"#SKU"))
               {
                   continue; //Skip as it does not contain #SKU
               }

               copy("/path/to/images/" . $file,"/path/to/images/backup/" . $file);

               /*Remove the #SKU*/
               $newf = str_replace("#SKU","",$file);

               /*Rename the old file accordingly*/
               rename("/path/to/images/" . $file,"/path/to/images/" . $newf);
          }
    }

    /*Close the handle*/
    closedir($handle);
}
那么,使用迭代器:

class SKUFilterIterator extends FilterIterator {
    public function accept() {
        if (!parent::current()->isFile()) return false;
        $name = parent::current()->getFilename();
        return strpos($name, 'SKU#') !== false;
    }
}
$it = new SkuFilterIterator(
    new DirectoryIterator('path/to/files')
);

foreach ($it as $file) {
    $newName = str_replace('SKU#', '', $file->getPathname());
    rename($file->getPathname(), $newName);
}
FilterEditor基本上过滤掉所有非文件,以及其中没有
SKU 35;
的文件。然后您所要做的就是迭代,声明一个新名称,并重命名文件

或在5.3+中使用新的:

小菜一碟:

foreach (array_filter(glob("$dir/WV1716B*.jpg") ,"is_file") as $f)
  rename ($f, str_replace("SKU#", "", $f));

(或
$dir/*.jpg
如果数字不重要)

您也可以使用此示例:

$directory = 'img';
$gallery = scandir($directory);
$gallery = preg_grep ('/\.jpg$/i', $gallery);
// print_r($gallery);

foreach ($gallery as $k2 => $v2) {
    if (exif_imagetype($directory."/".$v2) == IMAGETYPE_JPEG) {
        rename($directory.'/'.$v2, $directory.'/'.str_replace("#SKU","",$v2));
    }
}

您的意思是类似于重命名所有文件的循环?=1以获得更简单的解决方案。编辑:
array\u filte
r将第一个参数作为array.glob通常比目录迭代慢几倍-因此,如果您有很多文件-这是一件需要考虑的事情:此代码段工作正常,但它也重命名了文件的扩展名。你能解决它吗??
$directory = 'img';
$gallery = scandir($directory);
$gallery = preg_grep ('/\.jpg$/i', $gallery);
// print_r($gallery);

foreach ($gallery as $k2 => $v2) {
    if (exif_imagetype($directory."/".$v2) == IMAGETYPE_JPEG) {
        rename($directory.'/'.$v2, $directory.'/'.str_replace("#SKU","",$v2));
    }
}