Как изменить имя каждого файла в папке

Я пытаюсь изменить каждое имя файла в папке, например, если имя файла 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)

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

Другие решения

не уверен, что то же самое происходит в Windows, но в GNU здесь …

если вы распечатаете то, что намереваетесь сделать, вместо того, чтобы пытаться прямо, вы увидите некоторые недостатки:

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

когда, например делать:

echo ("rename( ".$fileName.", ".$newName.")\n");
  • Следующее, что нужно проверить, может быть, это права на изменение файлов …
2

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector