Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Upload Script - .JPG/.DOC corrupt

Status
Not open for further replies.

gbrian

IS-IT--Management
Sep 22, 2004
96
US
After using my script to upload files, some file types will become corrupt. PDF's work fine, text/html works fine..but files such as JPG/GIF/DOC, etc...become corrupt.

I am assuming this is a binary vs ascii issue and would like some help?

I've looked around for an answer to this and cannot find anything.

Here is my code:
if($HTTP_POST_FILES['userfile']['name'] <> "") {
//Process uploaded form
$uploadDir = $HTTP_SERVER_VARS['DOCUMENT_ROOT'] . '/documents/';
$uploadFile = $uploadDir . $HTTP_POST_FILES['userfile']['name'];
$fileName = $HTTP_POST_FILES['userfile']['name'];

$fileExist = mysql("$database", "select id from documents where fileName = '$fileName'");
$fileExist_rows = mysql_num_rows($fileExist);

if ($fileExist_rows > 0) {
echo "<font color=\"red\">The file '" . $fileName . "' already exists.</font>";
}else{

if (move_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'], $uploadFile)) {

echo "<font color=\"red\">Your document '". $HTTP_POST_FILES['userfile']['name'] . "' has been uploaded.</font>";
$add_doc = mysql("$database","insert into documents values('', '$description', '$fileName')");

}else{
print_r($HTTP_POST_FILES);
}
}
}else{
echo "<font color=\"red\">You must choose a file to upload.</font>";
}
 
I'm not sure what's wrong, but I doubt it's an 8-bit versus 7-bit problem, as PDFs are 8-bit files.

How are you testing that the files are corrupt?


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Well, when I try to view an image on the server it appears broken. Also, when trying to download a word doc that has been uploaded--Word doesn't recognize it as a valid file anymore.

 
Appears broken"?

You download the Word document, save it to the filesystem. Are the right number of bytes coming back from the server?


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Appears as a broken image in Internet Explorer.

Also, the file I just tested comes back 36 bytes larger. (90,672 to 90,708)
 
I just found something--it is placing

Content-Type: application/msword and 2 unprintable characters after it.

Why would it add this to the file?
 
I dunno. On what OS and web server are you running PHP?

Also, what are the settings in php.ini for file_uploads, upload_max_filesize, upload_tmp_dir, post_max_size and max_input_time?


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Apache/1.3.14 (Unix)
(Red-Hat/Linux)
PHP/4.0.4pl1

It places the Content-Type: attribute at the top of all files I upload.

IE: Content-Type: text/html when I upload an html file.

I have no idea why it is doing this.

 
Update:

Bug #9298 HTTP File upload, Content-Type header is stored at the beginning of the file
Submitted
Status: Closed
Version: 4.0.4pl1

It appears it is the version of PHP that we are running contained this bug.

I have not updated PHP to the latest version yet to test this--but I am willing to bet this is the issue!

Thanks for the help.
 
Yeah, I agree. I do not administrate the server and it is a development server (not live).


I have put in a request for updates ! Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top