I would like to print out the cause of the error.
error_get_last() doesn't seem to return anything. rename() returns TRUE|FALSE rather than an exception.
if (!rename($file->filepath, $full_path)) {
$error = error_get_last();
watchdog('name', "Failed to move the uploaded file from %source to %dest", array('%source' => $file->filepath, '%dest' => $full_path));
}
The rename() function in PHP is an inbuilt function which is used to rename a file or directory. It makes an attempt to change an old name of a file or directory with a new name specified by the user and it may move between directories if necessary.
Solution: We need to rename by appending increment number to the filename if file exists using PHP file_exists() function.
PHP rename() Functionrename("images","pictures"); rename("/test/file1. txt","/home/docs/my_file. txt");
First, it's a better practice to add some safety checks before:
if (file_exists($old_name) &&
((!file_exists($new_name)) || is_writable($new_name))) {
rename($old_name, $new_name);
}
Second, you can turn on error reporting:
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With