使用php在服务器上上传doc、docx、pdf文件
我希望在服务器上上传扩展名为doc、docx和pdf的文档。为此,我使用了以下代码,但我无法上传文件使用php在服务器上上传doc、docx、pdf文件,php,mysql,file-upload,upload,Php,Mysql,File Upload,Upload,我希望在服务器上上传扩展名为doc、docx和pdf的文档。为此,我使用了以下代码,但我无法上传文件 <?php if($_POST['save']) { $target_dir = "reqdoc/"; $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); $uploadOk = 1; $imageFileType = pat
<?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>
谁能告诉我代码哪里出错了吗?你忘了在表单标签中添加
enctype=“multipart/form data”
。试试这个
您的表单中缺少“enctype=“multipart/form data”
在编写客户端代码时,只需知道在表单包含任何元素时使用多部分/表单数据
使用if..elseif..elsead将enctype='multipart/formdata'
读取到表单中,这是一个名为MAX\u FILE\u SIZE
的隐藏字段,其大小(以字节为单位)作为值-这些应该会有所帮助
Sorry, file already exists.Sorry, only doc, docx, pdfSorry, your file was not uploaded.
<?php
if($_POST['save'])
{
$target_dir = "media/";
$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" enctype="multipart/form-data" 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>