Home > Article > Backend Development > How to find the cause of php rename error
How to find the cause of the php rename error: 1. Add some security checks; 2. Turn on the error report "error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);".
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
How to find the error in PHP:rename() reason?
I want to print out the reason for the error.
error_get_last() doesn't seem to return anything. rename() returns true false, not 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)); }
Solution
First of all, it is best to add some security checks before the following situations:
if (file_exists($old_name) && ((!file_exists($new_name)) || is_writable($new_name))) { rename($old_name, $new_name); }
Secondly, you can turn on error reporting:
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to find the cause of php rename error. For more information, please follow other related articles on the PHP Chinese website!