Page 1 of 1

type = "text/plain"

Posted: Mon Mar 08, 2010 10:37 am
by jaschulz
When I upload a text file with IE8, the file fails this test:

if ($_FILES["uploadFile"]["type"] == "text/plain") {

but when I upload the same file with Chrome, Firefox, Opera or Safari, it passes the test.

What's going on here?

Thanks,

JAS

Re: type = "text/plain"

Posted: Mon Mar 08, 2010 10:56 am
by AbraCadaver
$_FILES["uploadFile"]["type"] is not reliable because it is set by the browser and they may not do it the same way. IE8 probably sets it to application/octet-stream or something. You can use finfo_file() or mime_content_type().

Re: type = "text/plain"

Posted: Mon Mar 08, 2010 11:48 am
by jaschulz
I am very new at this. Can you show me the proper syntax for those alternatives (based on the model of what I was using)?

Thanks,

JAS

Re: type = "text/plain"

Posted: Mon Mar 08, 2010 1:17 pm
by AbraCadaver
Look at the manual for more examples:

Code: Select all

$type = mime_content_type($_FILES['uploadFile']['tmp_name']);
//or
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$type = finfo_file($finfo, $_FILES['uploadFile']['tmp_name']);

Re: type = "text/plain"

Posted: Mon Mar 08, 2010 1:44 pm
by jaschulz
OK. I used the mime_content_type alternative (finfo apparently requires php 5.3.0)

All is well on Chrome, Fifefox, Opera and Safari, but IE8 complains:

Warning: mime_content_type() [function.mime-content-type]: can only process string or stream arguments

That doesn't make any sense to me at all.

Any ideas?

Thanks.

Re: type = "text/plain"

Posted: Mon Mar 08, 2010 1:50 pm
by John Cartwright
You shouldn't be using mime_content_type() as it is deprecated. Try the fileinfo alternative (as suggested).

Re: type = "text/plain"

Posted: Mon Mar 08, 2010 1:52 pm
by AbraCadaver
jaschulz wrote:OK. I used the mime_content_type alternative (finfo apparently requires php 5.3.0)

All is well on Chrome, Fifefox, Opera and Safari, but IE8 complains:

Warning: mime_content_type() [function.mime-content-type]: can only process string or stream arguments

That doesn't make any sense to me at all.

Any ideas?

Thanks.
This shouldn't have anything to do with the browser. Show the code.

Re: type = "text/plain"

Posted: Mon Mar 08, 2010 3:08 pm
by jaschulz
Something is weirdly wrong. Here is my test code:

<?php

$mystring = "mystring = " . $_FILES["uploadFile"]["tmp_name"];
echo $mystring;

?>

It works fine in Chrome etc. But in IE 8 it returns only the constant text (i.e. "my string =") and nothing at all for $_FILES["uploadFile"]["tmp_name"].

My host (bluehost.com) is running php 5.2.1.

Can anyone tell me what's going on here?

Re: type = "text/plain"

Posted: Mon Mar 08, 2010 5:32 pm
by jaschulz
It turns out the problem is in my javascript. I am doing (or trying to do) something fancy to deal with the mess that the different browsers make of the HTML file input, and somehow that is preventing IE from actually sending the name of the file I am trying to upload. I'll try to figure this out tomorrow, and explain it in a new thread.

Thanks.