Log In · Register

 

Help Topic Rules and Requirements

For a list of all requirements and guidelines pertaining to posting a new Help topic, please click here.

This Month's Contests | Staff Member of the Month | Hosts Looking for Hostees | Hostees looking for Hosts | BigBookofResources

Submission Guidelines

 
Reply to this topicStart new topic
PHP Get file MIME content type
Mikeplyts
post Aug 30 2010, 09:57 PM
Post #1


Mel Blanc was allergic to carrots.
*******

Group: Official Designer
Posts: 6,371
Joined: Aug 2008
Member No: 676,291



So, I'm touching up on this little issue because I'm working on a file system sort of thing. Usually, when I have it set so the user can open files, I have this huge old list of cases where based on the extension of the file, the correct MIME type is used for the Content-Type header. I've come to a point where I really just don't want to waste time trying to create these long lists of MIME types and cases and such based on extensions. I must imagine there is some sort of way to get the correct MIME types based on the file specified and being able to assign that to a variable which in turn could be used in that Content-Type header.

I tried using the original function that I assumed would work:

CODE
<?php
$type = mime_content_type($path); // $path contains the full path to the specified file to be opened.
?>


But that didn't seem to work. I tried reading into some more stuff and found out it's deprecated, but everything else is for PHP 5. Unfortunately, my web host is providing me with PHP 4. Knowing, however, that mime_content_type() was created for PHP 4 and not working, I'm fairly stuck. I even went as far to edit my php.ini file but there was really nothing I saw to change in the configuration. Asking my host to switch me to PHP 5 is really pretty much a hassle for what I need to do, and besides, I'm afraid a lot of other codes were based in PHP 4 and switching to PHP 5 would f*ck them up.

So, any possible suggestions? I've tried looking into some other functions, but they're so damn long for finding out such a simple thing that I doubt I'd have to use them. Otherwise, hit me with your best shot.
 
mipadi
post Aug 31 2010, 09:04 AM
Post #2


Senior Member
******

Group: Administrator
Posts: 2,648
Joined: Apr 2008
Member No: 639,265



If you're on a Unix or Linux host and can execute shell commands with the backtick operator, you can use the file program to get MIME info:

CODE
$file_mime = `file --mime-type $path`;    // Returns a string like: 'path/to/file: text/plain'
$mime = explode(': ', $file_mime)[1];     // Now contains '/text/plain'
 
Mikeplyts
post Aug 31 2010, 04:19 PM
Post #3


Mel Blanc was allergic to carrots.
*******

Group: Official Designer
Posts: 6,371
Joined: Aug 2008
Member No: 676,291



^ That almost worked, but there seemed to be an error with this array key(?) beside the explode function.

CODE
[1]


I tried it without it, but it generated some headers errors. _unsure.gif

EDIT:

Never mind! I just removed that array key or whatever it was and then modified my code so that the headers wouldn't f*ck up. Thanks a lot, though! biggrin.gif
 

Reply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members: