MIME type couldn't be detected

See all posts Reply

MIME type couldn't be detected new!
by Chaman, 15 years, 3 months ago
Hi

When I force the download of a file using your suggested code (from http://www.verot.net/res/sources/class.upload.html) it usually works fine:

$handle = new upload($_FILES['image_field']);
header('Content-type: ' . $handle->file_src_mime);
header("Content-Disposition: attachment; filename=".rawurlencode($handle->file_src_name).";");
echo $handle->Process();
die();

However if I try to do it for a MS Publisher file (.pub) then it fails to identify the mime type:
Log output:
system information
- class version : 0.26
- GD version : 2.0.28
- supported image types : png jpg gif bmp
- open_basedir : /home/****/:/tmp:/usr/local/lib/php/
- language : en_GB
source is a local file /home/****/class_fans_9.pub
- local file name OK
- MIME type detected as by UNIX file() command
- MIME type detected as by browser
- Try to guess MIME type from file extension (pub): MIME type set to
- MIME type couldn't be detected!
- source variables
file_src_name : class_fans_9.pub
file_src_name_body : class_fans_9
file_src_name_ext : pub
file_src_pathname : /home/****/class_fans_9.pub
file_src_mime :
file_src_size : 75776 (max= 2097152)
file_src_error : 0


Is there any way to add/check mime types manually, as i guess you can't possibly cater for all the hundreds of other file types out there which aren't in the functions which are checking...

Thanks.

BTW I found I had to add the following to make downloads work in IE6:
header('Cache-Control: maxage=10'); 
header('Pragma: public');
header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H"), date("i"), date("s")+10, date("m"), date("d"), date("Y")))." GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Content-Length: ".$handle->file_src_size);
Reply
Re: MIME type couldn't be detected new!
by colin, 15 years, 3 months ago
Could you please send me by email one of these Publisher files? None of the MIME detection techniques seem to pick-up the MIME type, for some reason.

With the Publisher file, I will be able to do tests, and plan for this special case.Reply
Re: MIME type couldn't be detected new!
by Chaman, 15 years, 3 months ago
Example publisher file sent...Reply
Re: MIME type couldn't be detected new!
by colin, 15 years, 3 months ago
Received.

Yes, the files are weird. In fact, the MIME type is returned as \012- application/msword, which is invalid. The error seems to be with the file, as all the MIME detection techniques return the same thing. Maybe MS Publisher saves the files wrongly.

You can add this to your code so that the class accepts this invalid MIME type:
$handle->allowed = array('\012- application/msword');
Reply
Re: MIME type couldn't be detected new!
by Chaman, 15 years, 3 months ago
I have tried adding this mime type, but it doesn't seem to work.

I also found application/x-mspublisher from http://filext.com/file-extension/PUB

This is my code (iin case i#I've added the code in the wrong place):

$handle = new upload(PATH_TO_ROOT.$row['dfilename']); 
			
// attempted fixes for dodgy MS Publisher mime type
$handle->allowed = array('\012- application/msword');
$handle->allowed = array('application/x-mspublisher');
$handle->allowed = array('012- application/msword');
			
// extras
header('Cache-Control: maxage=10'); 
header('Pragma: public');
header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H"), date("i"), date("s")+10, date("m"), date("d"), date("Y")))." GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Content-Length: ".$handle->file_src_size);
			
header('Content-type: ' . $handle->file_src_mime);
header("Content-Disposition: attachment; filename=".rawurlencode($handle->file_src_name).";");
header("Content-Transfer-Encoding: binary\n");
			
// capture log ready to email 		
$_SESSION['error'] = $handle->log;
			
$to      = 'EMAIL_TO_ADMIN';
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";// To send HTML mail, the Content-type header must be set
$headers .= 'From: '.EMAIL_FROM_WEBSITE;
$subject = WEBSITE_NAME . ' fetch data';
$message = ''.$_SESSION['error'].'';
$ok = @mail($to, $subject, $message, $headers);

echo $handle->Process();
die();

If the detection continues to fail (or indeed another bad type fails detection) I guess I could just see if file_src_mime = '' and then just provide a direct link to the file instead?Reply
Re: MIME type couldn't be detected new!
by Chaman, 15 years, 3 months ago
I realise the allowed mime types should probably be written like this instead:

$handle->allowed = array('\012- application/msword',
'application/x-mspublisher','012- application/msword'');

This has the side effect of stopping say pdf files from being downloaded correctly....Reply
Re: MIME type couldn't be detected new!
by colin, 15 years, 3 months ago
Yes, you have to provide a list of MIME types, in an array.

Is it working now?Reply
Re: MIME type couldn't be detected new!
by Chaman, 15 years, 3 months ago
I think it has worked. It seemed to work for me using FF2, but someone else using IE6 was still having problems - that could be some caching issue though....

In the end (time was against me) I resorted to testing the mime type variable and if it was empty then I just display a direct link to the file with instructions to right-click and save as....Reply