|
Posted by Erwin Moller on 08/30/07 09:21
Erwin Moller wrote:
> Defacta wrote:
>> Hi,
>>
>> When I upload an excel file, PHP does not recognize the type of this
>> file:
>>
>> echo $HTTP_POST_FILES['file']['type'] ;
>>
>> returns: application/octet-stream
>> Whereas a word file returns: application/msword
>>
>> Do you know if there is anyway to get the type of a excel file ?
>>
>> Thanks,
>> Vincent.
>>
>
> Hi Vincent,
>
> From http://nl3.php.net/manual/en/features.file-upload.php
>
> ------------------------------------------------------------------------
> $_FILES['userfile']['type']
>
> The mime type of the file, if the browser provided this information.
> An example would be "image/gif". This mime type is however not checked
> on the PHP side and therefore don't take its value for granted.
> ------------------------------------------------------------------------
>
> Bottomline: don't rely on this.
> And the value application/octet-stream just means it is a binary file,
> which is correct in this case, but little informative.
Forgot to mention:
When I am in the situation my script must decide WHAT the file is, I
always use the extension to make my best guess.
But it is stays a guess.
You can never make this 100% foolproof.
If you upload a MS Word document to my Linux (Debian) server, I have no
way of checking what it is. I cannot open it for example.
You could rename your mydoc.pdf to mydoc.gif or mydoc.doc.
There ARE a few things you can check on a nix server to extract the
filetype, but it cannot handle all.
Here is a piece of code I use in some situation (*nix only)
if (!function_exists('mime_content_type')) {
function mime_content_type($f) {
$f = escapeshellarg($f);
return trim( `file -bi $f` );
}
}
and then call it like:
$probablemime = mime_content_type($yourfile);
where $yourfile must be a real file. Not just the name as handed to you
via $_FILES[] but the file itself. You can use it on the temporary
stored file before moving it.
Hope that helps a bit.
Regards,
Erwin Moller
Navigation:
[Reply to this message]
|