如何使用 PHP 上传 pdf 文件的代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25320735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:46:52  来源:igfitidea点击:

Codes in how to upload pdf file using PHP

phppdffile-upload

提问by Ejardy

I don't know the codes in how to upload the PDF file in PHP. Can I have a sample codes for it with a modified upload size? Any help is appreciated. Is it possible to save it to database and can delete it? Thanks!

我不知道如何在 PHP 中上传 PDF 文件的代码。我可以为它提供一个修改上传大小的示例代码吗?任何帮助表示赞赏。是否可以将其保存到数据库中并可以删除它?谢谢!

回答by Jose CC

There is a lot of tutorials out there. This tutorialworked for me.

那里有很多教程。本教程对我有用。

Step 1 – A basic user interface to select file

步骤 1 – 选择文件的基本用户界面

<form action="upload_file.php" method="post" enctype="multipart/form-data">

<input type="file" name="file" size="50" />

<br />

<input type="submit" value="Upload" />

</form>

Step 2 – Uploading file to server PHP script

步骤 2 – 上传文件到服务器 PHP 脚本

<?php

 $targetfolder = "testupload/";

 $targetfolder = $targetfolder . basename( $_FILES['file']['name']) ;

if(move_uploaded_file($_FILES['file']['tmp_name'], $targetfolder))

 {

 echo "The file ". basename( $_FILES['file']['name']). " is uploaded";

 }

 else {

 echo "Problem uploading file";

 }

 ?>

Limiting the file types / Uploading images with PHP script

限制文件类型/使用 PHP 脚本上传图像

<?php

 $targetfolder = "testupload/";

 $targetfolder = $targetfolder . basename( $_FILES['file']['name']) ;

 $ok=1;

$file_type=$_FILES['file']['type'];

if ($file_type=="application/pdf" || $file_type=="image/gif" || $file_type=="image/jpeg") {

 if(move_uploaded_file($_FILES['file']['tmp_name'], $targetfolder))

 {

 echo "The file ". basename( $_FILES['file']['name']). " is uploaded";

 }

 else {

 echo "Problem uploading file";

 }

}

else {

 echo "You may only upload PDFs, JPEGs or GIF files.<br>";

}

?>