/    Sign up×
Community /Pin to ProfileBookmark

Return file’s MIME type

Okay. All I want to do is get a file’s MIME type. Simple enough, right? Not exactly.

I have gone through httpd.conf and php.ini and enabled mime_magic or whatever it is, but now mime_content_type() returns absolutely nothing. Is there a different way to get the MIME type, or is there a way to get mime_content_type to work?

Thanks in advance,
Dan

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@bokehAug 23.2005 — mime_content_type() is deprecated. Its output is based on file extension only so it can be easily fooled. If you could be more specific about the file types you want to allow I could write something.
Copy linkTweet thisAlerts:
@Daniel_TauthorAug 23.2005 — I'm working on a sort of MP3 toolkit. It sets the ID3 tags based on the filename, and vice versa. The user specifies the folder, and then the script crawls the folder for MP3's. Currently it's just setup to use the file extension, but I'd prefer that it used MIME types.

So, to answer your question, MP3's.
Copy linkTweet thisAlerts:
@bokehAug 23.2005 — [code=php]<?php
// searches a directory for MP3s
// if a file has mp3 extension it
// checks first few bytes of the
// file to check it is an mp3

$dir = '/path/to/dir/'; // directory to search
$dir = opendir($dir);
while (false !== ($file = readdir($dir))) { // run through all files
if (eregi('.mp3{1}$', $file)){ // if file has mp3 extesion
$fp = fopen($file, 'rb'); // open file
if(fgets($fp, '4') == 'ID3'){ // read first few bytes and check is mp3
$mp3_array[] = $file; // if mp3 add file name to $mp3_array[]
}
fclose($fp);
}
}
closedir($dir);
?>[/code]
×

Success!

Help @Daniel_T spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...