简体   繁体   中英

PHP - fopen: failed to open stream: Permission denied

I'm new to PHP. I planned to create folder, sub folder, into that file depends on user Input.

Folder and sub folders has been created successfully.

Finally I try to create a file its showing bellow error.

fopen(upload/localhost/hrms): failed to open stream: Permission denied in C:\\xampp\\htdocs\\ssspider\\index.php on line 205

My code is:

$dir = "http://localhost:8080/hrms/index.php";

//make directory
$directoryForServer = "upload";
$directoryForClient = $directoryForServer."/".$host."";
mkdir($directoryForClient);


$splitePath = explode("/", $folderPath);

$folderPath1 = $directoryForClient;

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

writefile($folderPath1);



function writefile($dir)
{
 if( is_dir($dir)){
    echo $dir;

    $myFile = fopen($dir,"w");
    if($myFile)
    {
        fwrite($myFile, $returned_content);
    }
    fclose($myFile);
  }
}

Please help me to find out my problem?

Edit: Thanks. I got an error. In fopen I didn't mention file name . Now its working fine. Thanks

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);
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM