如何排查PHP rename函数执行失败的具体原因?
- 内容介绍
- 文章标签
- 相关推荐
本文共计434个文字,预计阅读时间需要2分钟。
输出错误原因的工具:使用 `error_get_last()` 函数。此函数返回最后一次发生的错误信息。
重命名文件操作:`rename($file, $filepath)` 返回 `true` 或 `false`,表示操作成功与否,而不是异常。
错误处理逻辑:如果 `rename()` 函数失败,则使用 `error_get_last()` 获取错误信息,并输出错误消息。
打印出错误的原因。
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)); }
解决办法
首先,最好在以下情况之前新增一些安全检查:
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);
知识点扩展:
语句:rename(oldname,newname,context)
注释:在 php 4.3.3 之前,rename() 不能在基于 *nix 的系统中跨磁盘分区重命名文件.
注释:用于 oldname 中的封装协议必须和用于 newname 中的相匹配.
注释:对 context 的支持是 php 5.0.0 添加的.
到此这篇关于php rename错误原因的查找方法的文章就介绍到这了,更多相关php rename错误原因内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!
本文共计434个文字,预计阅读时间需要2分钟。
输出错误原因的工具:使用 `error_get_last()` 函数。此函数返回最后一次发生的错误信息。
重命名文件操作:`rename($file, $filepath)` 返回 `true` 或 `false`,表示操作成功与否,而不是异常。
错误处理逻辑:如果 `rename()` 函数失败,则使用 `error_get_last()` 获取错误信息,并输出错误消息。
打印出错误的原因。
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)); }
解决办法
首先,最好在以下情况之前新增一些安全检查:
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);
知识点扩展:
语句:rename(oldname,newname,context)
注释:在 php 4.3.3 之前,rename() 不能在基于 *nix 的系统中跨磁盘分区重命名文件.
注释:用于 oldname 中的封装协议必须和用于 newname 中的相匹配.
注释:对 context 的支持是 php 5.0.0 添加的.
到此这篇关于php rename错误原因的查找方法的文章就介绍到这了,更多相关php rename错误原因内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

