My client uses a webhost running php 5.2.6, so fileinfo functions are not available. Mime-magic extension is not loaded, and use of dl() is denied.
At first, this project was going to deal with images, so I thought I might get away with using exif_imagetype() or getimagesize(). Now it has expanded to allowing file uploads of other formats as well.
Can you point me in the right direction?
How to get mime type of a file?
Moderator: General Moderators
- flying_circus
- Forum Regular
- Posts: 732
- Joined: Wed Mar 05, 2008 10:23 pm
- Location: Sunriver, OR
Re: How to get mime type of a file?
Looks like you'll have to roll your own.
The idea is to look at the first few bytes of a file and look them up in a table of sorts. It works for many types of files (images, PDF files, MS Office documents...) but there are some types where it doesn't always work.
FILExt.com is a good place to look.
The idea is to look at the first few bytes of a file and look them up in a table of sorts. It works for many types of files (images, PDF files, MS Office documents...) but there are some types where it doesn't always work.
FILExt.com is a good place to look.
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: How to get mime type of a file?
Well then your client has hamstrung you as the available tools aren't sufficient to meet the requirements. The best you can do here is exec() the linux 'file' command or if that's not available then build a solution that uses the file extension (not reliable).
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: How to get mime type of a file?
5.2.6 < 5.3.0
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
- flying_circus
- Forum Regular
- Posts: 732
- Joined: Wed Mar 05, 2008 10:23 pm
- Location: Sunriver, OR
Re: How to get mime type of a file?
While googling, I did find a little snippet that looked interesting. However, not being too familiar with *nix, was reluctant to actually use it.AbraCadaver wrote:Well then your client has hamstrung you as the available tools aren't sufficient to meet the requirements. The best you can do here is exec() the linux 'file' command or if that's not available then build a solution that uses the file extension (not reliable).
If you have any thoughts, I'd like to get your opinion, otherwise I will spend some time reading later this week.
Code: Select all
$file = escapeshellarg($filename);
$mime = shell_exec("file -bi " . $file); - AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: How to get mime type of a file?
Yes, that's exactly right, and as far as I know is the same mechanism as the PHP extensions use. There may also be some "better" or more concise magic files such as the ones that come with Apache. 'file' has always worked for me and seems to get most common file types. I know that it doesn't get Gimp .xcf.flying_circus wrote:While googling, I did find a little snippet that looked interesting. However, not being too familiar with *nix, was reluctant to actually use it.AbraCadaver wrote:Well then your client has hamstrung you as the available tools aren't sufficient to meet the requirements. The best you can do here is exec() the linux 'file' command or if that's not available then build a solution that uses the file extension (not reliable).
If you have any thoughts, I'd like to get your opinion, otherwise I will spend some time reading later this week.
Code: Select all
$file = escapeshellarg($filename); $mime = shell_exec("file -bi " . $file);
You can also use function_exists() or is_callable() to test for the preferred functions and if not found use 'file'.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Re: How to get mime type of a file?
Oh, wow. I could have sworn I used that in a PHP4 script once... sorry!AbraCadaver wrote:5.2.6 < 5.3.0