Getting the mime type of an uploaded file is easy enough:
echo mime_content_type($fileatt['tmp_name']);
However, I also want to check the mime-type of files which are included in a zipped file. After unzipping my files (looping through the files in the zip and where i
is the current file), I've tried:
$info = pathinfo($zip->getNameIndex($i));
echo mime_content_type($info['dirname'] . "\\" . $info['basename']);
which gives the error: Warning: mime_content_type() [function.mime-content-type]: File or path not found '.\foo.pdf' in C:\Users\<user>\<website>\validation.php
on line 56
I realised that the dirname
for a zipped file is relative to the zip file, not the absolute path, so I tried:
$a = pathinfo($fileatt['tmp_name']);
$b = $a['dirname'] . "\\" . $info['basename'];
echo mime_content_type($b);
which gives the error: Warning: mime_content_type() [function.mime-content-type]: File or path not found 'C:\xampp\tmp\foo.pdf' in C:\Users\<user>\<website>\validation.php
on line 56
Can anyone cast any light on the path of the file? (I suspect the answer might be the same as the comment on getting image height and width from zipped files, but are there any alternative methods?)
UPDATE
Thanks to Baba, the following does work:
$fp = fopen('zip://C:\Users\<user>\<website>\test.zip#foo.jpg', "r");
(n.b. I can only get this to work when I give the full route of the zip file, not a tmp file as would be the case when a file is uploaded via a form). However, trying to get the mime-type: echo mime_content_type($fp);
generates the error:
Warning: mime_content_type() [function.mime-content-type]: stream does not support seeking in C:\Users\<user>\<website>\includes\validation.php on line 70
This happens regardless of the file type (i.e. the problem stated in the only comment on http://php.net/manual/en/ziparchive.getstream.php doesn't seem to affect me).
Incidentally, this is also the same error I get when I try a different method: $fp = $zip->getStream('foo.jpg');
I know that there are several other 'stream not supported' questions on SO, but I couldn't work out how they related to my problem, and I was hoping that since this method has been specifically suggested someone might have a good answer...
(p.s. I'm not using the finfo_*
functions as my host currently refuses to install PHP v5.3).