所以我希望用户只能上传文档或 docx。所以首先这是我的html:
<form action="upload_file.php" method="post" enctype="multipart/form-data">
Select a file: <input type="file" name="img">
<input type="submit">
</form>
这是我的php:
$allowedExts = array("doc", "docx");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ($extension!=".doc" || $extension!=".doc"
&& ($_FILES["file"]["size"] < 200000)
&& 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("Proposals/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"Proposals/" . $_FILES["file"]["name"]);
echo "Stored in: " . "Proposals/" . $_FILES["file"]["name"];
}
}
} else {
echo "Invalid file";
}
每次我尝试上传文件时,无论是 doc 还是 png,它都会输出:
Upload:
Type:
Size: 0 Kb
Temp file:
already exists.
最终没有任何东西上传到提案文件夹。所以我有两个问题:
1)我的代码有什么问题?
2) 它重定向到upload_file.php 并显示一条消息。有没有办法真正返回主页并显示它成功的文本?