【问题标题】:Using php to rename all files in folder使用php重命名文件夹中的所有文件
【发布时间】:2012-08-10 22:09:15
【问题描述】:

这里有新的 php 程序员。我一直在尝试通过替换扩展名来重命名文件夹中的所有文件。

我使用的代码来自the answer to a similar question on SO.

if ($handle = opendir('/public_html/testfolder/')) {
while (false !== ($fileName = readdir($handle))) {
    $newName = str_replace(".php",".html",$fileName);
    rename($fileName, $newName);
}
closedir($handle);

}

运行代码时我没有收到任何错误,但文件名没有更改。

任何关于为什么这不起作用的见解?我的权限设置应该允许。

提前致谢。

编辑:我在检查 rename() 的返回值时得到一个空白页,现在尝试使用 glob() 进行某些操作,这可能是比 opendir 更好的选择...?

编辑 2:使用下面的第二个代码 sn-p,我可以打印 $newfiles 的内容。所以数组存在,但是str_replace + rename() sn -p 修改文件名失败。

$files = glob('testfolder/*');


foreach($files as $newfiles) 
    {

    //This code doesn't work:

            $change = str_replace('php','html',$newfiles);
    rename($newfiles,$change);

           // But printing $newfiles works fine
           print_r($newfiles);
}

【问题讨论】:

标签: php file rename


【解决方案1】:

您可能在错误的目录中工作。确保在 $fileName 和 $newName 前面加上目录。

特别是,opendir 和 readdir 不会传递有关当前工作目录的任何信息以进行重命名。 readdir 只返回文件名,而不是它的路径。所以你只是传递要重命名的文件名。

像下面这样的东西应该会更好:

$directory = '/public_html/testfolder/';
if ($handle = opendir($directory)) { 
    while (false !== ($fileName = readdir($handle))) {     
        $newName = str_replace(".php",".html",$fileName);
        rename($directory . $fileName, $directory . $newName);
    }
    closedir($handle);
}

【讨论】:

  • 您好 Telgin,感谢您的回答。不幸的是我试过了,结果一样,代码没有产生错误,但也没有变化。
  • @Munner 尝试检查重命名的返回值。如果它不能重命名文件,它应该返回 false。这将有助于缩小问题范围。
  • 代码运行良好,稍作调整:将其放在 whitle 循环中以排除当前和父文件夹:if($fileName == "." || $fileName == "..") continue;
【解决方案2】:

你确定吗

opendir($directory)

有效吗?你检查过吗?因为这里似乎可能缺少一些文档根...

我会试试的

$directory = $_SERVER['DOCUMENT_ROOT'].'public_html/testfolder/';

然后是Telgin的解决方案:

if ($handle = opendir($directory)) { 
    while (false !== ($fileName = readdir($handle))) {     
        $newName = str_replace(".php",".html",$fileName);
        rename($directory . $fileName, $directory . $newName);
    }
    closedir($handle);
}

【讨论】:

  • 非常感谢您的建议。到目前为止,我一直在尝试一堆没有运气的解决方案,我将尝试上述编辑并让您知道!
【解决方案3】:

这是一个简单的解决方案:

PHP 代码:

// your folder name, here I am using templates in root
$directory = 'templates/';
foreach (glob($directory."*.html") as $filename) {
    $file = realpath($filename);
    rename($file, str_replace(".html",".php",$file));
}

以上代码将转换.php

中的所有.html文件

【讨论】:

    【解决方案4】:

    如果文件被打开,就会发生这种情况。那么php就不能对文件做任何修改了。

    【讨论】:

      【解决方案5】:
      <?php
      $directory = '/var/www/html/myvetrx/media/mydoc/';
      if ($handle = opendir($directory)) { 
          while (false !== ($fileName = readdir($handle))) {
              $dd = explode('.', $fileName);
              $ss = str_replace('_','-',$dd[0]);
              $newfile = strtolower($ss.'.'.$dd[1]);
              rename($directory . $fileName, $directory.$newfile);
          }
          closedir($handle);
      }
      ?>
      

      非常感谢您的建议。它对我有用!

      【讨论】:

        猜你喜欢
        • 2020-05-17
        • 2013-04-24
        • 2018-11-30
        • 2014-08-03
        • 2017-10-02
        • 2018-05-19
        • 1970-01-01
        相关资源
        最近更新 更多