PHP : 重命名() 如何找到错误原因


PHP : rename() How do I find the error cause?

我想打印出错误的原因。

error_get_last() 似乎没有返回任何内容。 rename() 返回 TRUE|假而不是例外。

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));
}

首先,最好在以下之前添加一些安全检查:

if (file_exists($old_name) && 
    ((!file_exists($new_name)) || is_writable($new_name))) {
    rename($old_name, $new_name);
}

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

error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

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

执行此操作

的另一种方法是执行copy(),以使用消息复制到所需的新名称(您可以验证)...然后使用 unlink() 删除原始文件。

但是,是的,rename()完全按照您所说的去做,并且在不起作用时不会产生错误:)

您似乎正在尝试移动上传的文件,请改用move_uploaded_file。