PHP fopen() function does not create a file

  php

I am trying to create a file in my PHP application using the following code:

$file = @fopen(Data::$FILE_PATH, 'w'); //I also tried with 'x'
if(!$file)
{
    new ErrorReporter('Error 3');
    return;
}

It always displays the Error 3 text.

PHP script has chmod 777, I tried to make $FILE_PATH:

  • /var/www/data.json
  • /var/data.json
  • /root/data/data.json
  • /var/test/data.json

But none of the following worked, the file is never created.

I tried to make a file manually and I debugged it using the following function:

    private function debug()
    {
        $file = Data::$FILE_PATH;

        error_reporting(E_ALL);
        ini_set('display_errors', true);
        echo 'phpversion: ', phpversion(), "n";
        echo 'uname: ', php_uname("s r"), "n"; // name/release of the operating system
        //echo 'sapi: ', php_sapi(), "n";

        echo $file, file_exists($file) ? ' exists' : ' does not exist', "n";
        echo $file, is_readable($file) ? ' is readable' : ' is NOT readable', "n";
        echo $file, is_writable($file) ? ' is writable' : ' is NOT writable', "n";

        $fp = fopen($file, 'w');
        if ( !$fp ) {
            echo 'last error: ';
            var_dump(error_get_last());
        }
        else {
            echo "ok.n";
        }
        die('done');
    }

The file:

  • existed
  • is readable
  • ISN’T WRITABLE

The error that appears in the debug function is:
failed to open stream: Permission denied in /path/to/the/file on line 108

Line 108 content:

$fp = fopen($file, 'w');

But as I previously said, chmod of the script is 777 and $FILE_PATH file is 664.

Can anyone help me in this case?

Source: Ask PHP

LEAVE A COMMENT