【发布时间】: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);
}
【问题讨论】: