XAMPP重命名功能

时间:2013-03-18 01:07:15

标签: php

我正试图以这种方式编写批量重命名:

if ($handle = opendir('../../upload_files')) {
  while (false !== ($fileName = readdir($handle))) {
    $newName = str_replace(", ","_",$fileName);
    rename($fileName, $newName);
    $count++;
  }
  closedir($handle);
  echo $count." files renamed";
}

但是当我运行脚本时,我收到一个警告:

Warning: rename(..,..) [function.rename]: No error in E:\WEBS\rename.php on line 6

导致错误的原因是什么?

1 个答案:

答案 0 :(得分:0)

如果目标文件已存在,则PHP在Windows环境下会出现此类错误。

PHP 5.3 https://bugs.php.net/bug.php?id=48771存在一个已知错误,但有类似的错误消息。

我建议您尝试对代码进行以下修改(这取决于您的代码,只需进行一些更正)

$dir = "../../upload_files";
if ($handle = opendir($dir))
{
    while (false !== ($fileName = readdir($handle)))
    {
        if (!isset($count)) $count = 0;
        if ($fileName == ".." || $fileName == ".") continue;
        $newName = str_replace(", ","_",$fileName);
        copy($dir.$fileName, $dir.$newName);
        $count++;
    }
    closedir($handle);
    echo $count." files renamed";
}