Skip to content
Advertisement

List of file types php

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!

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement