PHP - fopen:无法打开流:权限被拒绝

PHP - fopen: failed to open stream: Permission denied

我是 PHP 的新手。我计划根据用户输入在该文件中创建文件夹、子文件夹。

文件夹和子文件夹已成功创建。

最后我尝试创建一个显示以下错误的文件。

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

我的代码是:

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

请帮我找出我的问题?

编辑:谢谢。我得到一个错误。在 fopen 中我没有提到文件名。现在它工作正常。谢谢

因为你打开的是目录,fopen函数只是打开文件。您的代码有错误,请参考以下内容:

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