我是PHP的新手。我打算创建文件夹,子文件夹,该文件取决于用户输入。
已成功创建文件夹和子文件夹。
最后,我尝试创建一个显示以下错误的文件。
fopen(upload / localhost / hrms):无法打开流:权限被拒绝 在第205行的C:\ xampp \ htdocs \ ssspider \ index.php
我的代码是:
$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我没有提到文件名。现在它的工作正常。感谢
答案 0 :(得分:0)
因为你打开一个目录,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);
}
}