John Davidson

PHP fopen() function does not create a file

0 comments
Message:


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?



My Answer:
echo '
';_ echo 'file_exists: ', file_exists($file) ? 'true' : 'false';_ echo '
';_ echo 'is_writable: ', is_writable($file) ? 'true' : 'false';_ echo '
';_ echo 'is_readable: ', is_readable($file) ? 'true' : 'false';_ echo '
';_ echo 'fileperms: ', substr(sprintf('%o', fileperms($file)), -4);_ }__And the output is:_ phpversion: 7.4.3_ file_exists: false_ is_writable: false_ is_readable: false_ fileperms: 0644__It seems like the file does not exist, and it is not writable or readable._What could be the issue here?

Rate this post

4 of 5 based on 1318 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us