我在Mac上使用XAMPP。我有以下脚本,我正在尝试使用它将文件上传到文件夹中:
upload.php
上传表单简单地如下所示:
我的htdocs文件夹结构如下:
当我运行脚本时,出现以下错误:
警告:move_uploaded_file(upload/dirt.png) [function.move-uploaded-file]:无法打开流:权限被拒绝在 /Applications/XAMPP/xamppfiles/htdocs/development/Templates/PHP&JS/Upload Files to Server/form upload/upload.php的第29行
警告:move_uploaded_file() [function.move-uploaded-file]:无法将'/Applications/XAMPP/xamppfiles/temp/phpAQWDB1' 移动到 'upload/dirt.png' 在/Applications/XAMPP/xamppfiles/htdocs/development/Templates/PHP&JS/Upload Files to Server/form upload/upload.php的第29行。存储在:upload/dirt.png
如何更改上传文件夹的属性以允许上传文件?是否有配置文件应该包含以启用此功能?
<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
上传表单简单地如下所示:
index.html
<html>
<body>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
我的htdocs文件夹结构如下:
-/htdocs
-index.html
-upload.php
-/upload
当我运行脚本时,出现以下错误:
警告:move_uploaded_file(upload/dirt.png) [function.move-uploaded-file]:无法打开流:权限被拒绝在 /Applications/XAMPP/xamppfiles/htdocs/development/Templates/PHP&JS/Upload Files to Server/form upload/upload.php的第29行
警告:move_uploaded_file() [function.move-uploaded-file]:无法将'/Applications/XAMPP/xamppfiles/temp/phpAQWDB1' 移动到 'upload/dirt.png' 在/Applications/XAMPP/xamppfiles/htdocs/development/Templates/PHP&JS/Upload Files to Server/form upload/upload.php的第29行。存储在:upload/dirt.png
如何更改上传文件夹的属性以允许上传文件?是否有配置文件应该包含以启用此功能?
chmod
命令来设置上传文件夹的适当权限。请参考 Apache Friends 的这个帖子。 - Anne