PHP Classes

How to use PHP to Detect File Type From Path - Metyl PHP MIME Type Check package blog

Recommend this page to a friend!
  All package blogs All package blogs   Metyl PHP MIME Type Check Metyl PHP MIME Type Check   Blog Metyl PHP MIME Type Check package blog   RSS 1.0 feed RSS 2.0 feed   Blog How to use PHP to Det...  
  Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  

Author:

Viewers: 138

Last month viewers: 3

Package: Metyl PHP MIME Type Check

Some applications need to determine the type of file to do specific file processing for each file, like processing image files.

The Metyl PHP MIME Type Check package can simplify that task by detecting the MIME type and encoding of the data, as well if the usual file name extension is one of the extensions allowed by your PHP application.

Read this short PHP tutorial to quickly learn how to use the Metyl PHP MIME Type Check package to detect the correct file type.




Loaded Article

How to Determine the Type of a File

Let's suppose there is a file that your PHP application needs to process. It has an unknown type of content that you need to determine. You know what its path is on the file system.

To solve this problem, simply state use this code in your application:

metyl::make($filePath);

The class will return a metyl object. This object contains a few properties of the file that give details of the file format, as you may see in the following example:

metyl Object
(
    [name] => Europa
    [baseName] => Europa
    [extension] => jpeg
    [mime] => image/jpeg; charset=binary
    [mimeType] => image/jpeg
    [encoding] => binary
    [message] => 
    [mappings] => Array
        (
            [0] => jpeg
            [1] => jpg
            [2] => jpe
            [3] => jfif
        )
    [errorsFound:protected] => 
)

Since no errors happened, the message object property is an empty string.

When metyl analyses the file using the PHP fileInfo extension, it retrieves the usual extensions (see mappings) tied to the analyzed MIME type. If the file had an extension, it should match one of these mappings.

How to Check if the File Has One of Several Expected File Formats 

Now let's suppose that you have to process a file in your PHP application, and you have the requirement that the file is in a compressed format.

There are a lot of flavors of compressed files. Let's stick to a small set of these compressed file formats. Metyl has an array class variable that allows developers to specify the allowed types.

 static protected $types =
       [ 'pdf' => ['pdf'],
         'zip' => ['gz','gtar','tar','zip',],
        ];

There is also another function to use for specifying what type of file we want:

metyl::confirm($filePath, 'zip');

Metyl analyses the file and checks if the extension associated with the detected file type matches one of the specified types.

The class returns an object that can now be checked by calling the method isValid() to determine if the file matches our type requirements.

This process is all correct and smooth, as long as the file has an official IANA MIME type.

A known glitch of this kind of detection is that the MIME type of a PHP file is text/x-php. That is not an official MIME type. So it returns ??? as file name extension.

Download and Install the Metyl PHP MIME Type Check Package Using PHP Composer

You can download by going to this download page to get the package code in an archive compressed file format.

You can also install this package using PHP Composer tool by going to the same download page and click on the Install with Composer button to find the specific instructions to install this package together with other packages that you may want to use in your PHP application.




You need to be a registered user or login to post a comment

Login Immediately with your account on:



Comments:

No comments were submitted yet.




  Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  
  All package blogs All package blogs   Metyl PHP MIME Type Check Metyl PHP MIME Type Check   Blog Metyl PHP MIME Type Check package blog   RSS 1.0 feed RSS 2.0 feed   Blog How to use PHP to Det...