【问题标题】:php rename() Device or resource busyphp rename() 设备或资源忙
【发布时间】:2012-01-28 03:26:42
【问题描述】:

我的代码出现以下错误:

警告:

[function.rename]:设备或资源忙于

if ($handle = opendir($temp_images)) {

        /* This is the correct way to loop over the directory. */
        while (false !== ($file = readdir($handle))) {
             if ($file == $file) {
             }
             $path = ''; 
             $dir_handle = opendir($path);
             chdir($path);
             $oldfile = $path.$file ;
             $newfile = $path.preg_replace('/[\\&\\%\\$\\ ]+/', '-', $file); // replace &%$ with a - 
    
             if(FALSE == rename($oldfile, $newfile)) 
             {
    
             }

        }

       closedir($handle);
}

有什么问题,我该如何解决?

【问题讨论】:

  • 仔细检查文件名。看起来它们不正确。
  • 文件名在 /temp-uploaded/ 很好
  • 你有两个文件名,都应该没问题。你没有透露他们的全名,所以我不能告诉你他们好不好。我只能说当文件名错误/指向错误时会发生此错误。
  • 所有文件名都在 /home/therealf/public_html/temp-uploaded/ $oldfile, $newfile 文件名在同一目录中
  • 只需将新旧文件名完整添加到您的问题中。你能在 shell 中执行重命名操作吗?

标签: php rename


【解决方案1】:

如果你看

man 2 rename 

http://linux.die.net/man/2/rename

你会看到

EBUSY - 重命名失败,因为 oldpath 或 newpath 是某个进程正在使用的目录(可能是当前工作 目录,或者作为根目录,或者因为它是打开的 用于读取)或正在被系统使用(例如作为挂载点),而系统认为这是一个错误。 (注意 没有要求在这种情况下返回 EBUSY 情况——无论如何进行重命名并没有错——但是如果系统不能返回 EBUSY 是允许的 否则处理这种情况。)

只需打印输出您要重命名的内容,您就会看到发生了什么

【讨论】:

  • EBUSY 是 rename() 系统调用返回的错误,然后由 php 解释器使用类似 strerror 函数的东西打印出来,它使用 EBUSY errno 并将其转换为 'device or resource is busy '
  • @HAS 只打印您正在使用的所有变量,尤其是 $oldfile 和 $newfile,您将看到 rename() 为何返回 EBUSY。
  • 我想说的是:只需添加类似 if ($file != "." || $file != "..") continue;一段时间后(readdir())
【解决方案2】:

尽量排除'.''..'

if($file == '.' || $file == '..') {
    continue;
}

它对我有用。希望它对你有用。谢谢

【讨论】:

    猜你喜欢
    • 2017-09-02
    • 1970-01-01
    • 2016-08-29
    • 2015-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-17
    相关资源
    最近更新 更多