Home  >  Article  >  Backend Development  >  How to find the cause of php rename error

How to find the cause of php rename error

藏色散人
藏色散人Original
2021-07-22 10:55:262752browse

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);".

How to find the cause of php rename error

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn