user153923
user153923

Reputation:

php processing enctype multipart/form-data

I'm working with this PHP File Upload example.

I see that $_FILES["file"]["type"] is the type of the uploaded file.

When they later put restrictions on the file uploads based on these types, it looks like they are all image types.

Here is the PHP:

<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Error: " . $_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 "Stored in: " . $_FILES["file"]["tmp_name"];
    }
  }
else
  {
  echo "Invalid file";
  }
?>

For their four (4) $allowedExts, they have defined six (6) $_FILES["file"]["type"].

I will need to modify my script to allow more extensions:

'txt', 'doc', 'docx', 'xls', xlsx', 'zip', 'pdf'

I did a search, trying to find out what types of files I will be needing to filter on, but the best I came across was HTML enctype Attribute, but it did not tell me what types I need to code for.

I was able to learn that these types are NOT the same as MIME types, so I can't just use them.

Where is the list of types that PHP has defined that I can filter on when restricting my uploads?

It is probably just as simple as knowing what PHP calls it.

Upvotes: 0

Views: 1056

Answers (1)

Florian Bussmann
Florian Bussmann

Reputation: 436

$allowedExts = array("gif", "jpeg", "jpg", "png", "txt", "doc", "docx", "xls", "xlsx", "zip", "pdf");

MIME-Types are as following

.txt  - text/plain
.doc  - application/msword
.docx - application/vnd.openxmlformats-officedocument.wordprocessingml.document
.xls  - application/vnd.ms-excel
.xlsx - application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
.zip  - application/zip
.pdf  - application/pdf

Upvotes: 2

Related Questions