NYCPHP Meetup

NYPHP.org

[nycphp-talk] Determining mime type of data not a file

Andrew Yochum andrew at plexpod.com
Wed Jan 25 11:01:28 EST 2006


On Wed, Jan 25, 2006 at 10:37:42AM -0500, Scott Mattocks wrote:
> Unless I am confused, the mime type isn't a property of the file it is a 
> property of the data. So I don't see why all of the mime type functions 
> and classes that I have found only work on files.

Well typically the file is on disk, and you need to get its mimetype.
The mimetype isn't embedded in the file at all - it needs to be guessed,
which is why the extension is called "MIME Magic".  Read the first bit
of:
    http://us3.php.net/manual/en/ref.mime-magic.php

That said, I believe you can make what you want happen a few ways:
1. Write the file to a temp file:
    $out = fopen($filename = tempnam("/tmp", 'mime'), 'w');
    fwrite($out, $file_contents);
    fclose($out);
    $mimetype = mime_content_type($filename);
    unlink($filename);
2. Use a fifo named pipe - this avoids disk usage if thats a concern for you:
    posix_mkfifo($filename = tempnam("/tmp", 'mime'), 0644);
    $out = fopen($filename, 'w');
    fwrite($out, $file_contents);
    fclose($out);
    $mimetype = mime_content_type($filename);
    unlink($filename);
3. Use wrappers... don't know off hand, but something tells me you could
finagle getting a string to act as a php resource somehow.  Anyone?
    
HTH,
Andrew
-- 
Andrew Yochum
Plexpod
andrew at plexpod.com
718-360-0879



More information about the talk mailing list