File excel not recognized
File excel not recognized
am 30.08.2007 10:42:16 von vincent.marguerit
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.
Re: File excel not recognized
am 30.08.2007 11:11:38 von Erwin Moller
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.
Regards,
Erwin Moller
Re: File excel not recognized
am 30.08.2007 11:21:26 von Erwin Moller
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
Re: File excel not recognized
am 30.08.2007 11:25:03 von Erwin Moller
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,
>>
>> 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
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
Re: File excel not recognized
am 30.08.2007 11:41:10 von vincent.marguerit
On 30 ao=FBt, 10:25, Erwin Moller
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 =3D escapeshellarg($f);
> > return trim( `file -bi $f` );
> > }
> > }
>
> > and then call it like:
> > $probablemime =3D 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
Anyway to get the original extension, maybe using javascript !
Thanks,
Vincent.
Re: File excel not recognized
am 30.08.2007 11:50:14 von Erwin Moller
Defacta wrote:
> On 30 août, 10:25, Erwin Moller
> 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.
>
Re: File excel not recognized
am 30.08.2007 12:09:46 von vincent.marguerit
On 30 ao=FBt, 10:50, Erwin Moller
wrote:
> Defacta wrote:
> > On 30 ao=FBt, 10:25, Erwin Moller
> > 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 val=
ue
> >>>> for granted.
> >>>> ------------------------------------------------------------ --------=
----
> >>>> Bottomline: don't rely on this.
> >>>> And the value application/octet-stream just means it is a binary fil=
e,
> >>>> 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 =3D escapeshellarg($f);
> >>> return trim( `file -bi $f` );
> >>> }
> >>> }
> >>> and then call it like:
> >>> $probablemime =3D mime_content_type($yourfile);
> >>> where $yourfile must be a real file. Not just the name as handed to y=
ou
> >>> 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/r=
ef.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 clientmachin=
e
>
>
>
> > 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.
Yes, I did it in PHP:
function fc_manage_uploaded_file() {
global $HTTP_POST_FILES ;
global $path_CMS ;
if($uniq_name_file == "") {
$uniq_name_file =3D uniqid("") ;
}
$ext =3D substr($HTTP_POST_FILES['file']['name'], -3 ) ;
if( ($ext == "doc") ||
($ext == "xls") ||
($ext == "pdf") ) {
move_uploaded_file($HTTP_POST_FILES['file']['tmp_name'],
$path_CMS."/include/files/".$uniq_name_file.$ext) ;
return $uniq_name_file.$ext ;
} else {
return "" ;
}
}
Good...
Vincent.