2009-07-30 89 views
9

我試圖調試一個巨大的,過時的(大約2001年)PHP Web服務,並遇到文件打開失敗。 fopen調用位於一個包含的模塊中,調用者正在記錄該文件無法打開,但沒有記錄任何理由。如何輸出PHP文件打開失敗的原因

實際執行開放的代碼是:

// Read the file 
    if (!($fp = @fopen($fileName, 'rb'))) { 
    $errStr = "Failed to open '{$fileName}' for read."; 
    break; // try-block 
    } 

我怎樣才能找出原因的fopen失敗了?

回答

1

偉大的答案已經給出,有關@ operator,但這裏的一對夫婦更多的信息,可能是有用的,無論是你或其他人:

  • 如果出於調試的目的,你需要禁用在@ operator,您可以安裝scream extension- 又見the manual -這是當你保持某種舊的應用程序中沒有得到很好的設計真正有用/編碼^^
  • 根據你的PHP configuation (如果track_errors選項被激活),您可能可以使用$php_errormsg來獲取最後一條錯誤消息。

考慮這段代碼:

// This file doesn't exist 
if ([email protected]('/tmp/non-existant-file.txt', 'r')) { 
    var_dump($php_errormsg); 
} 

// My Apache server doesn't have the right to read this file 
if ([email protected]('/tmp/vboxdrv-Module.symvers', 'w')) { 
    var_dump($php_errormsg); 
} 

你會得到這樣的:

string 'fopen(/tmp/non-existant-file.txt) [<a href='function.fopen'>function.fopen</a>]: failed to open stream: No such file or directory' (length=129) 

string 'fopen(/tmp/vboxdrv-Module.symvers) [<a href='function.fopen'>function.fopen</a>]: failed to open stream: Permission denied' (length=122) 

所以,真正的,有用的,有意義的,錯誤信息;-)

9

拿走@符號。

@符號會抑制錯誤消息,所以它會抑制函數通常會給出的錯誤。