php 重命名错误:系统找不到指定的文件.(代码:2)


php rename error: The system cannot find the file specified. (code: 2)

<?php
$dir = opendir('C:'Users'Prometheus'Desktop'milkmaid');
$i = 1;
// loop through all the files in the directory
while (false !== ($file = readdir($dir)))
{
        if ($file != "." && $file != "..") {
        $newName = $i.'.mp4';
        $oldname = $file;
        rename($oldname, $newName);
        $i++;
    }
}
?>

当我运行上面的脚本时,我收到以下错误:

系统找不到指定的文件。(代码:2)

$dir不是

字符串。您无法$file连接它。您需要将目录放在单独的变量中,并且不要忘记在目录和文件名之间放置一个/

rename()中添加$dir对我有用

<?php
$dir = opendir('C:'Users'Prometheus'Desktop'milkmaid');
$i = 1;
// loop through all the files in the directory
while (false !== ($file = readdir($dir)))
{
        if ($file != "." && $file != "..") {
        $newName = $i.'.mp4';
        $oldname = $file;
        rename($dir.$oldname, $dir.$newName);
        $i++;
    }
}
?>

像这样使用它:-

$directory = '/public_html/testfolder/';
$i=1;
    if ($handle = opendir($directory)) { 
        while (false !== ($fileName = readdir($handle))) {     
            $newName = $i.'.mp4';
            rename($directory . $fileName, $directory . $newName);
            $i++:
        }
        closedir($handle);
    }
这对

我有用

<?php
$counter = 1;
$dir = 'D:'files'; //path of folder
if ($handle = opendir($dir)) 
{
    while (false !== ($fileName = readdir($handle))) 
    {
        if($fileName != '.' && $fileName != '..')
        {
            $newName = $counter . " - " . $fileName;
            rename($dir."/".$fileName, $dir."/".$newName);
            $counter++;
        }
    }
    closedir($handle);
}
?>