2013-06-03 66 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

我希望這會有所幫助。

相關問題