php fopen 上的详细错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2470217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:35:35  来源:igfitidea点击:

Detailed error on fopen

phperror-handlingfopen

提问by Elitmiar

I'm using fopen to read from a file

我正在使用 fopen 从文件中读取

$fh = fopen($path, 'r') or die('Could not open file');

Now I contantly get error Could not open file. I checked the file path and even changed the permissions of the file to 777. Is there a way I can get a detailed error report as why the file can't be opened similar to mysql_error()?

现在我不断收到错误无法打开文件。我检查了文件路径,甚至将文件的权限更改为 777。有没有办法获得详细的错误报告,说明为什么无法打开类似于 mysql_error() 的文件?

回答by Pekka

Turn on error reporting, or, in a production environment (from PHP 5.2.0 onwards) you should also be able to use error_get_last().

打开错误报告,或者,在生产环境中(从 PHP 5.2.0 开始)您也应该能够使用error_get_last().

回答by VolkerK

For php versions prior to 5.2 (lacking error_get_last()) you can use track_errors.

对于 5.2 之前的 php 版本(缺少 error_get_last()),您可以使用track_errors

ini_set('track_errors', 1);
$fh = fopen('lalala', 'r');
if ( !$fh ) {
  echo 'fopen failed. reason: ', $php_errormsg;
}

see also: http://de.php.net/reserved.variables.phperrormsg

另见:http: //de.php.net/reserved.variables.phperrormsg

回答by Your Common Sense

Yes.
PHP has detailed error message for you.
You just have to turn it on.

是的。
PHP 为您提供了详细的错误消息。
你只需要打开它。

To dislay it on the screen add these 2 lines at the top of the script:

要在屏幕上显示它,请在脚本顶部添加以下两行:

ini_set('display_errors',1);
error_reporting(E_ALL);

Or if you want it to be logged instead,

或者,如果您希望将其记录下来,

ini_set('log_errors',1);
ini_set('display_errors',0);
error_reporting(E_ALL);

Also note that using die() is very bad practice.

另请注意,使用 die() 是非常糟糕的做法。

回答by Your Common Sense

$fh = fopen($path, 'r') or  die (error_get_last());