Reply to Re: File excel not recognized

Your name:

Reply:


Posted by Erwin Moller on 08/30/07 09:50

Defacta wrote:
> On 30 août, 10:25, Erwin Moller
> <Since_humans_read_this_I_am_spammed_too_m...@spamyourself.com> wrote:
>> Erwin Moller wrote:
>>> 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,
>>>> Fromhttp://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
>> One last addition:
>> If you want to use PECL, have a look at:http://nl3.php.net/manual/en/ref.fileinfo.php
>>
>> php.net claims it does a very good job.
>> I never used it though...
>>
>> Regards,
>> Erwin Moller
>
> Really strange, this returns:
> defacta-computer:/Volumes/Development/tmp vincent$ file -bi
> morgans_bottleList.xls
> application/msword
>
> Msword for a excel file ! Really strange...
>
> The fact is when you upload a file you don't have its extension
> anymore, you upload a file which name will be something like var/tmp/
> phpo6bTNj

???
What does $_FILES['userfile']['name'] give you?
On my machine it give the original filename, as found on the clientmachine.

>
> Anyway to get the original extension, maybe using javascript !

Nope, Javascript is intentionally crippled when it comes to fileuploads.
It cannot interact with fileuploads. :-/

You'll have to solve this serverside.

Good luck.

Regards,
Erwin Moller

>
> Thanks,
> Vincent.
>

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация