1

Is there a way to detect the mime type of a file without actually having an actual file, for example when you're generating the file and serving it as a download?

I'm currently using file extension sniffing from here: http://www.php.net/manual/en/function.mime-content-type.php#87856

I was just wondering if there was another way short of actually creating the file on the server and using FileInfo, mime_content_type(), or file

4
  • 3
    If you're generating the file, shouldn't you already know what the mime type is? Commented Jan 28, 2011 at 4:19
  • 1
    @Marc - agreed. Sort of a mysterious question here. Commented Jan 28, 2011 at 4:22
  • Yes, I should already know the mime type. I'm just being lazy, and don't feel like having to look up the mime type each time I create a file download. As in doing something like: echo output_file($filename, $content); exit; where output_file would handle all the headers for me. Commented Jan 28, 2011 at 4:31
  • I don't get it either. How many different file types could you possibly generate yourself that you can't keep track of what file type you're generating...? Commented Jan 28, 2011 at 6:40

5 Answers 5

2

Try the Fileinfo finfo_buffer() function:

$filename = 'image.jpg';
$contents = file_get_contents($filename);

$finfo = finfo_open(FILEINFO_MIME_TYPE);
var_dump( finfo_buffer($finfo, $contents) ); // string(10) "image/jpeg"

You do say "short of actually creating the file," so this seems to meet your requirements even though it uses Fileinfo.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah! I can't beleive I didn't see that. Thanks!
0

Have you tried writing it to a ramdisk - shouldn't have a significant speed penalty and you can use the standard functions

Comments

0

You might want to pipe it as such:

 $type = `echo $FILE_CONTENTS | file -bi -`

READ: This is a bad idea. Do not do this. 'Command line injection' (thanks to Andrew Moore for point this out.)

6 Comments

...but the file doesn't exist (mysteriously).
Yes Steve, and this approach doesn't require it to be... What is your point?
You don't need the actual file for this method. All you need is the file contents in $FILE_CONTENTS. Or is it that you don't have the file OR the file contents. What do you have then? Now I'm confused too.
-1: That's a terrible idea. Ever heard of command line injection?
Oh yeah. I guess that calls for an 'oops'.
|
0

If you know the type of file you're generating, just consult an array of known mime types and apply as appropriate.

1 Comment

I already mentioned in the question that this is the method I'm currently using. I'm wondering if there was a more reliable way, and one that I don't have to maintain myself.
0

you can check this :

 $filename = 'image.jpg'; 
 $filename2 = 'file.php'; 
 echo mime_content_type($filename) . "<br>";
 echo mime_content_type($filename2);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.