我希望在服务器上上传扩展名为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.
任何人都可以告诉代码出错的地方
答案 0 :(得分:0)
试试这个......
你的形式
错过了'enctype =“multipart / form-data”'您正在编写客户端代码,当您的表单包含任何元素时,您需要知道的是使用multipart / form-data。
http://www.faqs.org/rfcs/rfc1867.html
titleTextAttributes
答案 1 :(得分:-1)
您忘记将enctype="multipart/form-data"
放在表单标记中。