问题
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