問題描述
當我無法打開文件時,如何獲得比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
語句時,can't open file
返回給客戶端,幾乎沒用.如果我刪除它,則不會引發任何錯誤.如果我返回 $fh
,它是 FALSE
.我嘗試了本地文件名和絕對文件名.我的 index.html
文件位于我的hole 文件夾的子文件夾之一中.此外,我將 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)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!