0

I'm trying to change every file name in a folder, for e.g if file name is style.css than i want to rename it as style_[md5 value of style].css = style_a1b01e734b573fca08eb1a65e6df9a38.css

here is what I've tried

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);
}

Where am i wrong?

errors are

Access is denied. (code: 5)
The system cannot find the file specified. (code: 2)
Aniket
  • 33
  • 1
  • 4
  • Why do you want to do this? If you want to force cache to regenerate, there are better ways than this. – Peon Apr 27 '17 at 14:50
  • I just want to do it once, because we have lot of css and js file – Aniket Apr 27 '17 at 14:52
  • Possible duplicate of [Using php to rename all files in folder](http://stackoverflow.com/questions/11955323/using-php-to-rename-all-files-in-folder) – Vincent Teyssier Apr 27 '17 at 14:55

2 Answers2

2

not sure the same happens on a windows, but on a GNU here …

if you printed out what you intend to do instead of trying bluntly you'd see some flaws:

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

when e.g. doing:

echo ("rename( ".$fileName.", ".$newName.")\n");
  • next thing to check maybe is rights to change files …
vv01f
  • 362
  • 1
  • 4
  • 21
  • Yes, of course, he's trying to rename the current and parent directories of the script's working directory (not even the directory where CSS files are). – Álvaro González Apr 27 '17 at 16:24
0
// 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'));
}
Peon
  • 7,902
  • 7
  • 59
  • 100
Eng_Farghly
  • 1,987
  • 1
  • 23
  • 34