如何在PHP中仅上传pdf文件 [英] How to upload only pdf files in PHP

查看:363
本文介绍了如何在PHP中仅上传pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这肯定是一个重复的问题,但是我在这里有不同的要求.我想要的是,当用户单击上载"按钮时,出现的对话框仅允许用户仅上载PDF文件类型,而不能上载其他文件.

I know this must be a repeated question but I have a different requirement here. What I want is when a user clicks on the Upload button, the dialog-box that appears only allows the user to upload the PDF file type only and nothing else.

到目前为止,我所看到的是,一旦用户上传了文件,便完成了对文件类型的检查.我不想允许用户上传pdf以外的文件.

What I've seen so far is once the user uploads a file, then there is a check done for file-type. I don't want to allow the user to upload files other than the pdf.

<?php
$allowedExts = array("pdf");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if (($_FILES["file"]["type"] == "application/pdf") && ($_FILES["file"]["size"] < 200000) && in_array($extension, $allowedExts))
{
   if ($_FILES["file"]["error"] > 0)
   {
      echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
   }
   else
   {
      echo "Upload: " . $_FILES["file"]["name"] . "<br>";
      echo "Type: " . $_FILES["file"]["type"] . "<br>";
      echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
      echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

      if (file_exists("doc_libraray/" . $_FILES["file"]["name"]))
      {
         echo $_FILES["file"]["name"] . " already exists. ";
         header('Location: '.site_url());
      }
      else
      {
          move_uploaded_file($_FILES["file"]["tmp_name"],
      "doc_libraray/" . $_FILES["file"]["name"]);
          echo "Stored in: " . "doc_libraray/" . $_FILES["file"]["name"];
          header('Location: '.$newURL);
      }
    }
}
else
{
    echo "Invalid file";
}
?>

请提出一种实现方法!

推荐答案

您想要的是HTML5,它具有用于<input type="file" ... > HTML代码中的对话框的文件类型选择器.

What you want is HTML5 which has a file type selector used for the dialog box in the <input type="file" ... > HTML code.

请参阅 MDN文档.

示例:

<input type="file" name="pdf" id="pdf" accept="application/pdf" >

来自 https://stackoverflow.com/a/7575533/3536236 回答他们说这未被广泛接受" ,但事实并非如此,input accept现在在HTML5标记中已被广泛接受.

From https://stackoverflow.com/a/7575533/3536236 Please note that due to the age of this answer they state "this is not widely accepted" but that is not so, input accept is now very widely accepted in HTML5 markup.

这不是PHP的问题,因为PHP仅在文件上传后才可以播放. PHP也应该检查上传后的文件类型,因为可以接受HTML接受.

This is not a PHP issue as PHP can only play with the file once it's been uploaded. PHP should also check the file type once uploaded as the HTML accept can be duped.

要进一步说明-由于PHP仅在服务器端起作用,因此在 之前,PHP不能不能检查要上传的文件.因此,我建议使用HTML5标记来仅选择(看起来像)PDF的文件,然后在您的PHP脚本上传后可以检查文件类型的有效性,然后处理结果.

To further clarify - PHP can not check the file that is to be uploaded before it is uploaded because PHP works on the server side only. Hence I suggest using HTML5 markup to select only files that (look like) PDFs, then once uploaded your PHP script can check the file type validity and then handle the outcomes.

PHP在上载文件之前无权选择文件,这完全取决于最终用户浏览器和HTML.

PHP has no place in choosing the file before it is uploaded, that is entirely down to the end user browser and HTML.

这篇关于如何在PHP中仅上传pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆