所以我在我的网站上有一个文件上传部分,用户可以上传任何 doc 或 docx 文件夹。这是我的html代码:
<form action="upload_file.php" method="post" enctype="multipart/form-data">
Select a file: <input type="file" name="upload">
<input type="submit">
这是 upload_file.php 的代码:
<?php
session_start();
$allowedExts = array("doc", "docx");
$extension = end(explode(".", $_FILES["upload"]["name"]));
if (($_FILES["upload"]["size"] < 200000)
&& in_array($extension, $allowedExts)) {
if ($_FILES["upload"]["error"] > 0)
{
echo "Return Code: " . $_FILES["upload"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["upload"]["name"] . "<br />";
echo "Type: " . $_FILES["upload"]["type"] . "<br />";
echo "Size: " . ($_FILES["upload"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["upload"]["tmp_name"] . "<br />";
if (file_exists("Proposals/".$_SESSION["FirstName"] ."/" . $_FILES["upload"]["name"]))
{
echo $_FILES["upload"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["upload"]["tmp_name"],
"Proposals/". $_SESSION["FirstName"] ."/". $_FILES["upload"]["name"]);
echo "Stored in: " . "Proposals/". $_SESSION["FirstName"] ."/". $_FILES["upload"]["name"];
}
}
} else {
echo "Invalid file";
}
?>
但这不会上传任何内容,即使它回复了一条成功的消息。我在想这是因为它不允许我创建目录。上面的代码有什么问题,还是我必须添加更多代码才能创建一个目录。如果有帮助的话,这里是我的文件夹结构: