【发布时间】:2015-10-28 10:54:37
【问题描述】:
我希望在服务器上上传具有 doc、docx 和 pdf 扩展名的文档。为此,我使用了以下代码,但无法上传文件
<?php
if($_POST['save'])
{
$target_dir = "reqdoc/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if (file_exists($target_file))
{
echo "Sorry, file already exists.";
$uploadOk = 0;
}
if ($_FILES["fileToUpload"]["size"] > 500000)
{
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
if($imageFileType != "doc" && $imageFileType != "docx" && $imageFileType != "pdf" )
{
echo "Sorry, only doc, docx, pdf";
$uploadOk = 0;
}
if ($uploadOk == 0)
{
echo "Sorry, your file was not uploaded.";
}
else
{
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file))
{
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
}
else
{
echo "Sorry, there was an error uploading your file.";
}
}
}
?>
<p> </p>
<form class="form-horizontal" role="form" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
<input type="file" name="fileToUpload" id="fileToUpload">
<button type="submit" value="submit" name="save">Save</button>
</form>
我收到错误提示
Sorry, file already exists.Sorry, only doc, docx, pdfSorry, your file was not uploaded.
谁能告诉我代码哪里出错了
【问题讨论】:
-
使用 if..elseif..else
-
将
enctype='multipart/formdata'添加到您的表单中,这是一个名为MAX_FILE_SIZE的隐藏字段,其大小(以字节为单位)作为值 - 这些应该会有所帮助
标签: php mysql file-upload upload