php 使用php重命名文件夹中的所有文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11955323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using php to rename all files in folder
提问by Munner
new php programmer here. I have been trying to rename all the files in a folder by replacing the extension.
新的 php 程序员在这里。我一直在尝试通过替换扩展名来重命名文件夹中的所有文件。
The code I'm using is from the answer to a similar question on SO.
我使用的代码来自对 SO 上类似问题的回答。
if ($handle = opendir('/public_html/testfolder/')) {
while (false !== ($fileName = readdir($handle))) {
$newName = str_replace(".php",".html",$fileName);
rename($fileName, $newName);
}
closedir($handle);
}
}
I get no errors when running the code, but no changes are made to the filenames.
运行代码时我没有收到任何错误,但没有对文件名进行任何更改。
Any insight on why this isn't working? My permission settings should allow it.
关于为什么这不起作用的任何见解?我的权限设置应该允许它。
Thanks in advance.
提前致谢。
EDIT: I get a blank page when checking the return value of rename(), now trying something with glob() which might be a better option than opendir...?
编辑:我在检查 rename() 的返回值时得到一个空白页,现在尝试使用 glob() 的东西,这可能是比 opendir 更好的选择......?
EDIT 2: With the 2nd code snippet below, I can print the contents of $newfiles. So the array exists, but the str_replace + rename() snippet fails to change the filename.
编辑 2:使用下面的第二个代码片段,我可以打印 $newfiles 的内容。因此该数组存在,但 str_replace + rename() 片段无法更改文件名。
$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);
}
回答by Neeraj Singh
Here is the simple solution:
这是简单的解决方案:
PHP Code:
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));
}
Above code will convert all .htmlfile in .php
上面的代码将转换所有.html文件.php
回答by Telgin
You're probably working in the wrong directory. Make sure to prefix $fileName and $newName with the directory.
您可能在错误的目录中工作。确保在 $fileName 和 $newName 前面加上目录。
In particular, opendir and readdir don't communicate any information on the present working directory to rename. readdir only returns the file's name, not its path. So you're passing just the file name to rename.
特别是,opendir 和 readdir 不会传达有关当前工作目录的任何信息以进行重命名。readdir 只返回文件的名称,而不是它的路径。所以你只传递文件名来重命名。
Something like below should work better:
像下面这样的东西应该工作得更好:
$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);
}
回答by m7o
Are you sure that
你确定吗
opendir($directory)
works? Have you checked that? Because it seems there might be some Document Root missing here...
作品?你检查过吗?因为这里似乎缺少一些文档根目录......
I would try
我会尝试
$directory = $_SERVER['DOCUMENT_ROOT'].'public_html/testfolder/';
And then Telgin's solution:
然后是 Telgin 的解决方案:
if ($handle = opendir($directory)) {
while (false !== ($fileName = readdir($handle))) {
$newName = str_replace(".php",".html",$fileName);
rename($directory . $fileName, $directory . $newName);
}
closedir($handle);
}
回答by Sithija Darshana
That happens if the file is opened. Then php cannot do any changes to the file.
如果打开文件,就会发生这种情况。然后 php 无法对文件进行任何更改。
回答by Susanta
<?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);
}
?>
Thank you so much for the suggestions. it's working for me!
非常感谢您的建议。它对我有用!

