views:

260

answers:

1

Hi all,

I am writing a small script to upload and detect MIME type, using Zend Framework. For the design purpose, I can't use Zend_Form but normal instead. And I simply apply from the manual :

$adapter = new Zend_File_Transfer_Adapter_Http(); $files = $adapter->getFileInfo(); $mime = $files->getMimeType();

But the system inform that the funcion getMimeType() does not existed. Then, I tried:

$adapter = new Zend_File_Transfer(); $files = $adapter->getFileInfo(); $mime = $files->getMimeType();

This time, it didn't work either. So, how can I get the MIME type ?

Thank you so much for your help

A: 

I believe $files in both of your snippets would just be a simple array, and each element should contain a key to indicate the type, like so:

$files = $adapter->getFileInfo();

foreach ($files as $file) {
    // Print the MIME Type for $file
    echo $file['type'];
}

The Zend_File_Transfer_Adapter_Abstract class defines the code for determining the MIME type. It depends on Fileinfo class/extension or the mime_content_type() function (part of an older version of Fileinfo, I believe).

http://us2.php.net/manual/en/book.fileinfo.php

PHP 5.3 includes Fileinfo by default. Versions prior to 5.3 require the use of the PECL extension:

http://pecl.php.net/package/Fileinfo

awgy
Hi awgy,Thanks for your tip, I tried, it worked but even I uploaded a PDF or JPEG file, it still recognized as "application/octet-stream". I am developing on MAC, but I did test on a CentOS box with PHP 5.2.x. Do I need to install anything to recognize a correct MIME type ?
mrblue
Added additional information on the MIME detection support -- let me know if that doesn't work for you.
awgy
Hi awgy,While I couldn't install Fileinfo package on MAC, I tried on my CentOS box, and it still only recognize a PDF file as "application/octet-stream". I tried to restart Apache to try again but can't get it. Thank you so much.
mrblue