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
PHP Import Excel into Database (Xls & Xlsx)
When to Wrap Curly Braces Around a Variable
What's the Best Way to Get the Fractional Part of a Float in PHP
Sending Data Along with a Redirect in Codeigniter
Google API How to Connect to Receive Values from Spreadsheet
Prevent Session Expired in PHP Session for Inactive User
Remove Null, False, and '' - But Not 0 - from a PHP Array
At' Symbol Before Variable Name in PHP: @$_Post
Allowing Users to Refresh Browser Without the "Confirm Form Resubmission" Pop-Up
If You Create a Variable Inside a If Statement Is It Available Outside the If Statement
Interview Question: in PHP, Is 123==0123
How to Escape String from PHP for JavaScript