Php: Fopen() Permission Denied

PHP - fopen: failed to open stream: Permission denied

because you open a directory , fopen function just open file. your code is fill with error , just Refer to the following:

<?php  

//make directory
$host = "aa"; //define $host variable
$directoryForServer = "upload";
$directoryForClient = $directoryForServer."/".$host."";
@mkdir($directoryForClient);

$splitePath = explode("/", $directoryForClient);
$folderPath1 = $directoryForClient;

for($x = 1; $x <= (count($splitePath)-1) ; $x++)
{
$folderPath1 = $folderPath1."/".$splitePath[$x];
echo "<br>".$folderPath1." - successfully created<br>2";
@mkdir($folderPath1);
}

writefile($folderPath1);

function writefile($dir)
{
if( is_dir($dir)){
echo $dir;
$myFile = fopen("upload/aa.txt","w");
if($myFile)
{
$returned_content = "hello world"; //define variable and his content before write to file.
fwrite($myFile, $returned_content);
}
fclose($myFile);
}
}

fopen() fails to open stream: permission denied, yet permissions should be valid

Check if the user that PHP runs under have "X" permission on every directory of the file path.

It will need it to access the file

If your file is: /path/to/test-in.txt
You should have X permission on:

  • /path
  • /path/to

and read permission on /path/to/test-in.txt

PHP function.fopen failed to open stream: Permission denied

Obviously PHP doesn't have write access to the file.
This being on Ubuntu PHP runs as the same user as Apache, so make sure the file is writable by the www-data group.

user@host:/path/to/your/file# chgrp www-data yourfile.txt
user@host:/path/to/your/file# chmod g+w yourfile.txt


Related Topics



Leave a reply



Submit