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

时间:2016-07-10 07:17:49

标签: php

<?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)

4 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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

<?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++;
    }
}
?>

答案 2 :(得分:0)

像这样使用它:-

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

答案 3 :(得分:0)

这对我有用

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