Page 1 of 2

image vs .doc upload

Posted: Tue Jan 06, 2004 9:40 am
by melindaSA
I have a file upload script that works fine when I upload images, but will not work if I try to upload a MS word document, can someone please take a look at my code and tell me what I am doing wrong:

Here is the original image upload script:

Code: Select all

<?php
$path = "/usr/local/psa/home/vhosts/xxxx.org/xxxdocs/xxxxx/images/";
$max_size = 200000;

if (!isset($HTTP_POST_FILES['userfile'])) exit;

if (is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'])) {

if ($HTTP_POST_FILES['userfile']['size']>$max_size) { echo "<b>The file is too big</b><br><br>\n"; exit; }
if (($HTTP_POST_FILES['userfile']['type']=="image/gif") || ($HTTP_POST_FILES['userfile']['type']=="image/pjpeg") || ($HTTP_POST_FILES['userfile']['type']=="image/jpeg")) {

if (file_exists($path . $HTTP_POST_FILES['userfile']['name'])) { echo "The file already exists<br>\n"; exit; }

$res = copy($HTTP_POST_FILES['userfile']['tmp_name'], $path .
$HTTP_POST_FILES['userfile']['name']);
if (!$res) { echo "<b>upload failed!</b><br><br>\n"; exit; } else { echo "<b>Upload Successful</b><br><br>\n"; }

echo "File Name: ".$HTTP_POST_FILES['userfile']['name']."<br>\n";
echo "File Size: ".$HTTP_POST_FILES['userfile']['size']." bytes<br>\n";
echo "File Type: ".$HTTP_POST_FILES['userfile']['type']."<br>\n";
} else { echo "<b>Wrong file type</b><br><br>\n"; exit; }

}


?>
Here is the changed code for the MS word upload:

Code: Select all

<?php

$path = "/usr/local/psa/home/vhosts/xxxx.org/xxxdocs/xxxApplication/resume/";
$max_size = 200000;

if (!isset($HTTP_POST_FILES['userfile'])) exit;

if (is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'])) {

if ($HTTP_POST_FILES['userfile']['size']>$max_size) { echo "<b>The file is too big</b><br><br>\n"; exit; }
if (($HTTP_POST_FILES['userfile']['type']=="text/plain")) { {

if (file_exists($path . $HTTP_POST_FILES['userfile']['name'])) { echo "The file already exists<br>\n"; exit; }

$res = copy($HTTP_POST_FILES['userfile']['tmp_name'], $path .
$HTTP_POST_FILES['userfile']['name']);
if (!$res) { echo "<b>upload failed!</b><br><br>\n"; exit; } else { echo "<b>Upload Successful</b><br><br>\n"; }

echo "File Name: ".$HTTP_POST_FILES['userfile']['name']."<br>\n";
echo "File Size: ".$HTTP_POST_FILES['userfile']['size']." bytes<br>\n";
echo "File Type: ".$HTTP_POST_FILES['userfile']['type']."<br>\n";
} else { echo "<b>Wrong file type</b><br><br>\n"; exit; }

}


?>
All I changed was the $path and image/gif to text/plain.

HELP!
Thank you,

Posted: Tue Jan 06, 2004 10:02 am
by redmonkey
The mime type of a MS word document is 'application/msword' not 'text/plain'

However, the mime-type is passed by the browser and some of them are not too good at picking up the mime-type so it may still fail even with the correct mime-type definition in the script.

Posted: Tue Jan 06, 2004 10:14 am
by JayBird
in what way does it fail?

Posted: Tue Jan 06, 2004 10:15 am
by melindaSA
I changed the type to 'application/msword' but I still get a blank page when I try to upload.

Is there a better way to do a MS work upload??

Thanks

Posted: Tue Jan 06, 2004 10:18 am
by JayBird
what output do you get if you put this in your script

Code: Select all

echo "<pre>";
print_r($HTTP_POST_FILES);
echo "</pre>";
Also, what version of PHP you using, if you are one of the more recent versions you should use $_FILES instead of $HTTP_POST_FILES

Mark

Posted: Tue Jan 06, 2004 10:38 am
by redmonkey
I could be wrong as the way you have formatted your code makes it difficult to read but....

Code: Select all

if (($HTTP_POST_FILES['userfile']['type']=="text/plain")) { {
Note the double opening of the curly braces?

Although not as good as accurately determining the mime-type you could validate the file based on file extension.

Posted: Tue Jan 06, 2004 10:41 am
by melindaSA
I added following to my script and I still get a blank screen.

Code: Select all

<?php
echo "<pre>"; 
print_r($HTTP_POST_FILES); 
echo "</pre>"; 

?>
you can test the upload function at http://www.bjrh.org/HRApplication/resume_upload.php

I am using PHP4.3.4.

Posted: Tue Jan 06, 2004 10:43 am
by JayBird
just tried your script, i don't get a blank screen, i get a message saying wrong file type.

Mark

Posted: Tue Jan 06, 2004 10:43 am
by melindaSA
Okay, it was the extra {, but I now get the error message, wrong file type

Posted: Tue Jan 06, 2004 10:57 am
by redmonkey
Change..

Code: Select all

} else { echo "<b>Wrong file type</b><br><br>\n"; exit; }
to...

Code: Select all

} else { echo "<b>Wrong file type, uploading of file types {$HTTP_POST_FILES['userfile']['type']} not allowed.</b><br><br>\n"; exit; }
That should at least tell you what mime-type your browser thinks it is (probably 'application/octet-stream').

Posted: Tue Jan 06, 2004 11:01 am
by melindaSA
I get:
Wrong file type, uploading of file types application/msword not allowed.
Weird!! :roll:

Posted: Tue Jan 06, 2004 11:03 am
by Nay
mMm, really wierd. Is the code there what you have now? If not, please let us know the code your testing out 'now'.

-Nay

Posted: Tue Jan 06, 2004 11:05 am
by melindaSA
Here is what I am using now:

Code: Select all

<?php
$path = "/usr/local/xxx/xxxxx/xxhosts/xxx.org/xxxdocs/xxxApplication/resume/";
$max_size = 200000;


if (!isset($_FILES['userfile'])) exit;

if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {

if ($FILES['userfile']['size']>$max_size) { echo "<b>The file is too big</b><br><br>\n"; exit; }
if (($FILES['userfile']['type']=="application/msword")) {

if (file_exists($path . $_FILES['userfile']['name'])) { echo "The file already exists<br>\n"; exit; }
echo "<pre>";
print_r($_FILES);
echo "</pre>";
$res = copy($_FILES['userfile']['tmp_name'], $path .
$HTTP_POST_FILES['userfile']['name']);
if (!$res) { echo "<b>upload failed!</b><br><br>\n"; exit; } else { echo "<b>Upload Successful</b><br><br>\n"; }

echo "File Name: ".$_FILES['userfile']['name']."<br>\n";
echo "File Size: ".$_FILES['userfile']['size']." bytes<br>\n";
echo "File Type: ".$_FILES['userfile']['type']."<br>\n";

} else { echo "<b>Wrong file type, uploading of file types {$HTTP_FILES['userfile']['type']} not allowed.</b><br><br>\n"; exit; }


}
?>

Posted: Tue Jan 06, 2004 11:09 am
by Nay

Code: Select all

($FILES['userfile']['size']>$max_size) { echo "<b>The file is too big</b><br><br>\n"; exit; }
if (($FILES['userfi
That part is wrong. Should be $_FILES. And if you're going to use $_FILES, use it throughout the whole code. (eg: HTTP_FILES)

-Nay

Posted: Tue Jan 06, 2004 11:09 am
by JayBird
in some places you have $FILES instead of $_FILES, which is why your test for doc type isn't working.

Mark