Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of file types php

Tags:

php

file-type

I'm looking for a list/description detailing the file type php assigns based on an uploaded file.

I have the following script. It takes the uploaded file, and the file type. Compares it to the array to check if the type is allowed and then proceeds with the script.

$file  = $_FILES['attachment']['tmp_name'];

//check file is correct type
$fileType = $_FILES['attachment']['type'];
// array of allowed file extensions
$fileTypeArray = ["application/pdf", "application/doc", "application/docx", "application/rtf", "application/txt", "application/odf", "application/msword"];


// now check that the file type matches that of the array. 
if (!in_array($fileType, $fileTypeArray)){ 

I would like to allow the upload of any doc, pdf, txt, etc. Its purpose is to handle uploaded cv's. Please tell me if you can think of a better solution. Cheers!

like image 915
atoms Avatar asked Feb 24 '15 21:02

atoms


People also ask

How many types of files are there in PHP?

The filetype() function accepts the filename as a parameter and returns one of the seven file types on success and False on failure. The seven possible return values of the filetype() function are: file: regular file.

How many types of files are there?

There are two types of files. There are Program files and Data Files.


1 Answers

These media types are in fact MIME types and PHP manages them (following excerpt taken from MDN)

A media type (also known as a Multipurpose Internet Mail Extensions or MIME type) is a standard that indicates the nature and format of a document, file, or assortment of bytes. It is defined and standardized in IETF's RFC 6838.

The Internet Assigned Numbers Authority (IANA) is responsible for all official MIME types, and you can find the most up-to-date and complete list at their Media Types page.

There are a few of them, and the list is subject to expansion.


For your comparison, I think you want to manually define the acceptable types, as using a generic types list would result in all types being acceptable (which does not seem to be your goal)

I'd also add the link towards the input man page and suggest you read about the accept attribute, which can filter your files right on client side

If the value of the type attribute is file, this attribute indicates the types of files that the server accepts; otherwise it is ignored. The value must be a comma-separated list of unique content type specifiers:

Don't let the text fool you; users won't be able to select the file if it's not in accepted file types, but do note that the server needs to validate that as well as it is possible to submit a form to a server directly and bypass any client validation.

like image 61
Félix Adriyel Gagnon-Grenier Avatar answered Sep 22 '22 16:09

Félix Adriyel Gagnon-Grenier