PHP:rename()如何找到错误原因?

Int*_*ted 7 php

我想打印出错误的原因.

error_get_last()似乎没有返回任何内容.rename()返回TRUE | FALSE而不是异常.

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));
}
Run Code Online (Sandbox Code Playgroud)

alf*_*sin 5

首先,在以下情况之前添加一些安全检查是更好的做法:

if (file_exists($old_name) && 
    ((!file_exists($new_name)) || is_writable($new_name))) {
    rename($old_name, $new_name);
}
Run Code Online (Sandbox Code Playgroud)

其次,您可以打开错误报告:

error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
Run Code Online (Sandbox Code Playgroud)


Int*_*ted 5

答案是“另一个错误处理程序”正在根据http://php.net/manual/en/function.error-get-last.php的 php 手册中的注释捕获错误。在本例中,它是 drupal 错误处理程序,错误被捕获到那里的错误日志中。

  • 应该说明您在问题中使用了框架。框架会为您做很多事情,并且经常会改变您收到的建议。 (3认同)