How to upload only pdf files in PHP

Abhishek

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.

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";
}
?>

Please suggest a way to achieve it!

Martin

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

please see MDN documentation.

Example:

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

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.

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.

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 has no place in choosing the file before it is uploaded, that is entirely down to the end user browser and HTML.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

How to upload multiple files in android

来自分类Dev

How to upload files use ExpressJS 4?

来自分类Dev

How to restrict Shiny fileInput to text files only?

来自分类Dev

Gulp: how to watch multiple files and perform a task for only the changes files?

来自分类Dev

I have a lot of netcdf files, how would I use xarray to upload all the files into one python notebook?

来自分类Dev

How to get perforce to pull only files that don't exist in workspace

来自分类Dev

Fastest way to upload text files into HDFS(hadoop)

来自分类Dev

Upload binary files using jquery and Spring MVC

来自分类Dev

Upload Multiple Files and Download its Zip

来自分类Dev

使用NodeJS的Slack API(files.upload)

来自分类Dev

PDF下载提供了损坏的PDF php

来自分类Dev

How to git-add only non-whitespace changes and new files?

来自分类Dev

How can I prevent a PDF file from being downloaded or printed with PHP or JavaScript?

来自分类Dev

pdf涉及php imagick错误

来自分类Dev

pdf regexp在php上匹配

来自分类Dev

PHP + html网页转pdf

来自分类Dev

识别PHP中的PDF / A文件

来自分类Dev

使用PHP从PDF提取图像

来自分类Dev

在php中显示pdf的内容

来自分类Dev

更新php版本pdf错误

来自分类Dev

pdf文件的php编码问题

来自分类Dev

PHP - 创建 PDF 剪裁框

来自分类Dev

空 $_FILES PHP

来自分类Dev

PHP file upload not working for mp4

来自分类Dev

WordPress async-upload.php 403错误

来自分类Dev

PHP UPLOAD_ERR_CANT_WRITE

来自分类Dev

PHP Upload IF ISSET总是这样说吗?

来自分类Dev

使用php处理提交为整个pdf的pdf表单

来自分类Dev

PHP在特定页码上将多个PDF插入PDF