繁体   English   中英

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM