Home >Backend Development >PHP Problem >What to do if the php fopen function fails

What to do if the php fopen function fails

藏色散人
藏色散人Original
2021-03-29 10:02:163019browse

The solution to the failure of the php fopen function: first add a PHP error report through "set_error_handler("customError");"; then modify the restrictions on PHP's access to the directory in the php configuration file php.ini.

What to do if the php fopen function fails

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

PHP fopen fails and returns false

I encountered a problem on December 14th. I just took some time to record it today. I hope it will be helpful to other friends who encounter this problem.

$fH = fopen("/tmp/test.log",'a');

In the above line of code, fopen actually returns false. It is neither a directory or file permissions-related issue, nor a file path issue (relative path, absolute path), etc., nor is it an issue with the mode of opening the file (r, w ,a etc.). After searching online for a while, I found that the reason for returning false is simply one of the three situations above, so I couldn't find the answer.

So I added a PHP error report:

//error handler function
function customError($errno, $errstr){ 
         echo "<b>Error:</b> [$errno] $errstr";
 }
//set error handler
set_error_handler("customError");

I found the reason immediately:

It turns out that I made the PHP access directory in the php configuration file php.ini Restrictions:

open_basedir= .

[Recommended learning: PHP video tutorial]

The above is the detailed content of What to do if the php fopen function fails. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn