title: How to upload only pdf files in PHP
body: <p>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. </p>
  
  <p>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.</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  $allowedExts = array("pdf");
  $temp = explode(".", $_FILES["file"]["name"]);
  $extension = end($temp);
  if (($_FILES["file"]["type"] == "application/pdf") &amp;&amp; ($_FILES["file"]["size"] &lt; 200000) &amp;&amp; in_array($extension, $allowedExts))
  {
     if ($_FILES["file"]["error"] &gt; 0)
     {
        echo "Return Code: " . $_FILES["file"]["error"] . "&lt;br&gt;";
     }
     else
     {
        echo "Upload: " . $_FILES["file"]["name"] . "&lt;br&gt;";
        echo "Type: " . $_FILES["file"]["type"] . "&lt;br&gt;";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB&lt;br&gt;";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "&lt;br&gt;";
  
        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";
  }
  ?&gt;
  </code></pre>
  
  <p>Please suggest a way to achieve it!</p>