设置上传文件的格式pdf
时间: 2023-06-14 09:08:06 浏览: 84
要设置上传文件格式为PDF,您可以通过以下步骤进行设置:
1. 在HTML表单中添加一个文件上传控件:
```
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload PDF" name="submit">
</form>
```
2. 在服务器端的PHP脚本中,检查上传的文件是否为PDF格式:
```
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$fileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if file is a PDF
if($fileType != "pdf") {
echo "Sorry, only PDF files are allowed.";
$uploadOk = 0;
}
```
3. 如果文件格式为PDF,则将文件移动到上传目录中:
```
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.";
}
}
```
这样,您就可以确保只允许上传PDF文件了。