php rename() Device or resource busy

ε祈祈猫儿з 提交于 2019-12-17 19:32:09

问题


I have code that gives the following error:

Warning:

[function.rename]: Device or resource busy in

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

What's the problem and how do I fix it?


回答1:


if you look at

man 2 rename 

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

you will see

EBUSY - The rename fails because oldpath or newpath is a directory that is in use by some process (perhaps as current working directory, or as root directory, or because it was open for reading) or is in use by the system (for example as mount point), while the system considers this an error. (Note that there is no requirement to return EBUSY in such cases — there is nothing wrong with doing the rename anyway — but it is allowed to return EBUSY if the system cannot otherwise handle such situations.)

just print output what you are trying to rename and you will see what is happening




回答2:


Try to exclude '.' and '..'.

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

It works for me. Hopefully, it will work for you. Thanks



来源:https://stackoverflow.com/questions/8638587/php-rename-device-or-resource-busy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!