2013-06-03 62 views
0

在我的剧本,我需要删除一个文件,可能会或可能不会在那里:如何在PHP中禁止某些E_WARNING的日志记录?

unlink($path); 

像实际的unlink(2),PHP's unlink()将取消链接进入,如果有。但是,如果不是,PHP会在E_WARNING级别记录无用的(以我的目的)消息...我想,这对一些人很好,但不适合我:(

用C语言编程,我可以在这种情况下,检查errno并简单地忽略ENOENT。在PHP中可以做什么 - 如何抑制此警告的日志记录?

我不想在尝试解除链接之前检查文件 - 这样做会增加另一个文件系统遍历比化妆品以外,没有理由:

if (file_exists($path)) 
    unlink($path); 

有没有更好的办法

? 0
+6

您可以用@前缀表达式来抑制仅用于该表达式的警告(例如@unlink($ path))。 – garlon4

+0

更好地处理错误比压制它。但是,PHP的错误报告设置可能很有用:http://php.net/manual/en/function.error-reporting.php – showdev

+2

是的,在这种情况下,只需使用错误抑制运算符“@”。但要小心养成习惯 - 这种情况是例外,而不是规则。 – Jon

回答

3

您可以用@前缀表达式来抑制仅用于该表达式的警告(例如,@unlink($path);)。

1

php.ini configuratioin

我同意大家谁提到使用“@”来压制错误。

您还可以更改php.ini文件中的一些设置,以避免错误显示出来。

; Error Level Constants: 
; E_ALL    - All errors and warnings (includes E_STRICT as of PHP 6.0.0) 
; E_ERROR   - fatal run-time errors 
; E_RECOVERABLE_ERROR - almost fatal run-time errors 
; E_WARNING   - run-time warnings (non-fatal errors) 
; E_PARSE   - compile-time parse errors 
; E_NOTICE   - run-time notices (these are warnings which often result 
;      from a bug in your code, but it's possible that it was 
;      intentional (e.g., using an uninitialized variable and 
;      relying on the fact it's automatically initialized to an 
;      empty string) 
; E_STRICT   - run-time notices, enable to have PHP suggest changes 
;      to your code which will ensure the best interoperability 
;      and forward compatibility of your code 
; E_CORE_ERROR  - fatal errors that occur during PHP's initial startup 
; E_CORE_WARNING - warnings (non-fatal errors) that occur during PHP's 
;      initial startup 
; E_COMPILE_ERROR - fatal compile-time errors 
; E_COMPILE_WARNING - compile-time warnings (non-fatal errors) 
; E_USER_ERROR  - user-generated error message 
; E_USER_WARNING - user-generated warning message 
; E_USER_NOTICE  - user-generated notice message 
; E_DEPRECATED  - warn about code that will not work in future versions 
;      of PHP 
; E_USER_DEPRECATED - user-generated deprecation warnings 
; 
; Common Values: 
; E_ALL & ~E_NOTICE (Show all errors, except for notices and coding standards warnings.) 
; E_ALL & ~E_NOTICE | E_STRICT (Show all errors, except for notices) 
; E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR (Show only errors) 
; E_ALL | E_STRICT (Show all errors, warnings and notices including coding standards.) 
; Default Value: E_ALL & ~E_NOTICE 
; Development Value: E_ALL | E_STRICT 
; Production Value: E_ALL & ~E_DEPRECATED 
; http://php.net/error-reporting 
error_reporting = E_ALL 

这最后一行error_reporting允许您准确地更改要显示的错误。在你的情况下,E_WARNING错误是你想要避免的,所以我会使用E_ALL & ~E_WARNING

我希望这会有所帮助。

相关问题