2
votes
'<?php
    $allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000)
&& 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("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?> '

I've tried application/pdf....How to upload pdfs? I can get jpg and png to upload on my website, but I can't for doc, txt, nor pdf. I know that this code is for $allowedExts = array("jpg", "jpeg", "gif", "png"); only, but how to change the || ($_FILES["file"]["type"] == "image/jpeg") to || ($_FILES["file"]["type"] == "application/pdf")? Thanx in advance...

1

1 Answers

3
votes

Your code is looking for extensions(pdf) && Types

Mime types for pdfs:

application/pdf, application/x-pdf, application/acrobat, applications/vnd.pdf, text/pdf, text/x-pdf

Extensions and mime types for Microsoft docs: http://filext.com/faq/office_mime_types.php


I think you can fix the problem with that:

Modify your page adding this line

echo "Type: " . $_FILES["file"]["type"] . "<br> Extension: " . $_FILES["file"]["name"] . "<br>";

It will show the file type and name.