When trying to use the PHP rename function with two valid paths (source and target), I get a warning with the following message:
PHP Warning: rename(/u01/www/user/project/image.jpg,/mnt/files/archive/image.jpg): No data available
The weird thing is that the file is actually copied (not moved, the original file still exists) but has 0 bytes and is corrupted.
Tried to Google this error, but can't find any information. Maybe a permissions issue?
CodePudding user response:
Solution1
please check folder is writable or not.
is_writable('/mnt/files/archive/image.jpg').
Enable error reporting
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
Now add some safety checks before:
$old_name='/u01/www/user/project/image.jpg';
$new_name='/mnt/files/archive/image.jpg';
if (file_exists($old_name) &&
((!file_exists($new_name)) || is_writable($new_name))) {
rename($old_name, $new_name);
}
CodePudding user response:
Solution2
use copy() function instaed of rename.
$old_name='/u01/www/user/project/image.jpg';
$new_name='/mnt/files/archive/image.jpg';
if ( copy($old_name, $new_name) ) {
echo "Copy success!";
}else{
echo "Copy failed.";
}