Get meaningful information when fopen() fails (PHP/suPHP)

前端 未结 2 542
独厮守ぢ
独厮守ぢ 2020-12-11 16:41

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(         


        
相关标签:
2条回答
  • 2020-12-11 16:48

    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.

    (*) I consider it good practice to always set an error handler that turns all PHP errors into exceptions.

    0 讨论(0)
  • 2020-12-11 16:48

    Use error_get_last() to catch the (supressed) errors in php:

    $f = @fopen("x", "r") or die(print_r(error_get_last(),true));
    
    0 讨论(0)
提交回复
热议问题