【问题标题】:How to change name of every file in folder如何更改文件夹中每个文件的名称
【发布时间】:2017-04-28 14:22:38
【问题描述】:

我正在尝试更改文件夹中的每个文件名,例如,如果文件名是 style.css,我想将其重命名为 style_[md5 value of style].css = style_a1b01e734b573fca08eb1a65e6df9a38.css

这是我尝试过的

if ($handle = opendir("D:/web/htdocs/extra/css/")) {
    while (false !== ($fileName = readdir($handle))) {
        $path_parts = pathinfo($fileName);
        $newName = md5($path_parts['filename']);
        rename($fileName, $newName);
    }
    closedir($handle);
}

我哪里错了?

错误是

Access is denied. (code: 5)
The system cannot find the file specified. (code: 2)

【问题讨论】:

  • 为什么要这样做?如果你想强制缓存重新生成,还有比这更好的方法。
  • 我只想做一次,因为我们有很多css和js文件

标签: php css file-rename batch-rename


【解决方案1】:

不确定在 Windows 上是否会发生同样的情况,但在 GNU 上……

如果你打印出你打算做什么而不是直截了当地尝试,你会发现一些缺陷:

rename( ., d41d8cd98f00b204e9800998ecf8427e)                                                                                                                                                     
rename( .., 5058f1af8388633f609cadb75a75dc9d) 

当例如正在做:

echo ("rename( ".$fileName.", ".$newName.")\n");
  • 接下来要检查的可能是更改文件的权限……

【讨论】:

  • 是的,当然,他正在尝试重命名脚本工作目录的当前目录和父目录(甚至不是 CSS 文件所在的目录)。
【解决方案2】:
// DS to print  \  the split between folder
define('DS',DIRECTORY_SEPARATOR);
// APP_PATH to get application path on the the server
define('APP_PATH',__DIR__.DS);

$oldname = APP_PATH.'css'.DS.'style.css';
/*
when you echo $oldname ,you will get the complete path of file
*/
// check the file is exists or No
if (file_exists($oldname)) {
        $newName = md5($oldname);
        /*add the extension of file that you will rename it */
        rename($oldname, ($newName.'.css'));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-08
    • 2017-11-13
    • 2012-02-02
    • 2019-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多