fopen()失败时获取有意义的信息(PHP / suPHP) [英] Get meaningful information when fopen() fails (PHP/suPHP)

查看:164
本文介绍了fopen()失败时获取有意义的信息(PHP / suPHP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我无法打开文件时,如何获得比FALSE更有意义的内容。

How do I get something more meaningful than 'FALSE' when I can't open a file.

$myFile = "/home/user/testFile.txt"; 
$fh = fopen($myFile, 'w') or die("can't open file");

当我使用 die 语句时, code>无法打开文件返回给客户端,几乎没用。如果我删除它,没有错误提出。如果我返回 $ fh ,它是 FALSE 。我尝试了本地文件名和绝对文件名。我的 index.html 文件位于我的孔文件夹的一个子文件夹中。此外,我正在使用suPHP与我要写入的文件夹,具有 0755 的权限(suPHP要求所有文件夹)。

When I use the die statement, can't open file is returned to the client, and it is almost useless. If I remove it, no error is raised. If I return $fh it is FALSE. I tried both local file name and absolute file name. My index.html file is in one of the sub folders of my hole folder. Furthermore, I am using suPHP with the folder I am trying to write to having a permission of 0755 (suPHP requires this for all folders).

如何找出为什么有问题,或至少在尝试打开文件之前查询。

How do I figure out why there was a problem, or at least query it before trying to open the file.

推荐答案

fopen 如果失败,应该引发E_WARNING。请参阅 error_get_last set_error_handler (*)来捕获它。除此之外,您可以使用 file_exists is_readable 来检查文件是否丢失或有另一个(可能与权限相关)的问题。

fopen should raise an E_WARNING if it fails. See error_get_last or set_error_handler(*) to catch it. Other than that you can use file_exists and is_readable to check whether the file is missing or there's another (probably permission-related) problem.

(*)我认为很好的做法是始终设置一个错误处理程序,将所有PHP错误转换为异常。

(*) I consider it good practice to always set an error handler that turns all PHP errors into exceptions.

这篇关于fopen()失败时获取有意义的信息(PHP / suPHP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆