Why is there a permission warning, but still successfully opened the file?

I tried to open the file with the fopen () function in PHP, and it displays a warning about refusing to open the stream: permission denied. You know that warning / error that you encounter when apache does not have sufficient privileges to open a specific file.

However, despite the warning message being displayed, my PHP script successfully opened the file and wrote a line into it. It does not make sense.

So what is it? I can put @ directly in front of fopen (), but it's still weird, and I want to know why PHP behaves this way. Is there something that I did not configure correctly?

class XMLDB {

    private $file = null;
    private $xml = null;
    private $defs = array();
    private $recs = array();

    // private members above, public members below

    public function __construct($xmlfile) {
        if (!file_exists($xmlfile)) {
            die('XML file does not exist.');
        }
        $this -> file = $xmlfile;
        $this -> xml = simplexml_load_file($this -> file);
        $this -> iniVocab();
        $this -> iniData();
    }

... / * many private and public functions * /

    public function commit() {
        $xmlfile = fopen($this -> file, 'w'); // this is causing the warning
        $doc = new DOMDocument('1.0');
        $doc -> preserveWhiteSpace = false;
        $doc -> loadXML($this -> xml -> asXML());
        $doc -> formatOutput = true;
        fwrite($xmlfile, $doc->saveXML());
    }

    public function __destruct() {
        $this -> commit();
        /* comment this line out and there won't be any warnings, 
        /* therefore it should trace back to here. So I found out that
        /* it when I use die() that eventually calls __destruct()
        /* which in turn calls commit() to trigger this fopen warning. */
    }
}

EDIT: , - , . , , , , , __destruct() $this β†’ commit() - , . .

+3
4

, fclose.

, , . .

:

public function commit() {
    $xmlfile = fopen($this -> file, 'w');
    $doc = new DOMDocument('1.0');
    $doc -> preserveWhiteSpace = false;
    $doc -> loadXML($this -> xml -> asXML());
    $doc -> formatOutput = true;
    fwrite($xmlfile, $doc->saveXML());
    fclose($this -> file);
}
0

, " " , , ? ? , , , .

0

Try using file_put_contents () .... but I believe that it will give the same warnings ...

0
source

Does a warning provide you when you do this outside of __destruct? Maybe this is due to this. Or does apache have write permissions but cannot read?

0
source

Source: https://habr.com/ru/post/1716896/


All Articles