When handling file uploads, it is often important to determine the type of file being uploaded. While some files may be easily recognizable based on their contents, others may prove to be hard to identify.
MagicDb
is a file database that consists of specifications for several file formats. This recipe shows us how to use this database, through CakePHP's MagicDb
class, to properly identify files uploaded by our users.
The license for the MagicDb
database file allows its use only on open source or freely available software. If you wish to identify files on commercial applications, you will have to find a different approach.
As we will be working on files uploaded by our users, we need to build a form to upload files. We will store these uploads in a table, so create this table with the following SQL statement:
CREATE TABLE `uploads`( `id` INT UNSIGNED AUTO_INCREMENT NOT NULL, `file` VARCHAR(255) NOT NULL, `mime` VARCHAR(255) default NULL, `description...