PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
melindaSA
Forum Commoner
Posts: 99 Joined: Thu Oct 02, 2003 7:34 am
Post
by melindaSA » Tue Jan 06, 2004 9:40 am
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,
redmonkey
Forum Regular
Posts: 836 Joined: Thu Dec 18, 2003 3:58 pm
Post
by redmonkey » Tue Jan 06, 2004 10:02 am
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.
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Tue Jan 06, 2004 10:14 am
in what way does it fail?
melindaSA
Forum Commoner
Posts: 99 Joined: Thu Oct 02, 2003 7:34 am
Post
by melindaSA » Tue Jan 06, 2004 10:15 am
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
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Tue Jan 06, 2004 10:18 am
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
redmonkey
Forum Regular
Posts: 836 Joined: Thu Dec 18, 2003 3:58 pm
Post
by redmonkey » Tue Jan 06, 2004 10:38 am
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.
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Tue Jan 06, 2004 10:43 am
just tried your script, i don't get a blank screen, i get a message saying wrong file type.
Mark
melindaSA
Forum Commoner
Posts: 99 Joined: Thu Oct 02, 2003 7:34 am
Post
by melindaSA » Tue Jan 06, 2004 10:43 am
Okay, it was the extra {, but I now get the error message, wrong file type
redmonkey
Forum Regular
Posts: 836 Joined: Thu Dec 18, 2003 3:58 pm
Post
by redmonkey » Tue Jan 06, 2004 10:57 am
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').
melindaSA
Forum Commoner
Posts: 99 Joined: Thu Oct 02, 2003 7:34 am
Post
by melindaSA » Tue Jan 06, 2004 11:01 am
I get:
Wrong file type, uploading of file types application/msword not allowed.
Weird!!
Nay
Forum Regular
Posts: 951 Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia
Post
by Nay » Tue Jan 06, 2004 11:03 am
mMm, really wierd. Is the code there what you have now? If not, please let us know the code your testing out 'now'.
-Nay
melindaSA
Forum Commoner
Posts: 99 Joined: Thu Oct 02, 2003 7:34 am
Post
by melindaSA » Tue Jan 06, 2004 11:05 am
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; }
}
?>
Nay
Forum Regular
Posts: 951 Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia
Post
by Nay » Tue Jan 06, 2004 11:09 am
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
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Tue Jan 06, 2004 11:09 am
in some places you have $FILES instead of $_FILES, which is why your test for doc type isn't working.
Mark