Forum Moderators: coopster

Message Too Old, No Replies

PHP file uploading problems still

         

RussellC

11:11 pm on Mar 13, 2003 (gmt 0)

10+ Year Member



I posted a while ago about this and gave up on the issue but I am trying at it again. I created an upload script and it works fine for plain text file but dosnt work for anything binary. Here is the basic code and the output. I have set my max file size etc high enough in php.ini etc...

<?php
if (!isset($changestatus)) {
?>

<form method="POST" action="<?=$PHP_SELF?>" enctype="multipart/form-data" />
<input name="userfile" type="file" />
<input name="changestatus" type="submit" value="SUMBIT" />
</form>

<?php
} else {

if ($userfile!= "" ¦¦ $userfile!= NULL) {
echo("Error Code: " . $_FILES['userfile']['error'] . "<br />");
echo("File Size: " . $_FILES['userfile']['size'] . "<br />");
echo("MIME Type: " . $_FILES['userfile']['type']);
move_uploaded_file($userfile, "/var/www/html/filelistings/" . $_FILES['userfile']['name']);
echo("<br />Done");
}
}
?>

Now when I run it the file uploads fine, but the file size is about twice the size of the original file unless it is a plain/text file. .txt files work fine.

This is the output I get uploading a 156129 byte image:

In IE6:

Error Code: 0
Temp Name: /tmp/phpWzn1Gb
File Size: 311777
MIME Type: image/pjpeg
Done

In Mozilla:

Error Code: 0
Temp Name: /tmp/phpr9vlnL
File Size: 312405
MIME Type: image/jpeg
Done

In Opera:

Error Code: 0
Temp Name: /tmp/phpP7GxxO
File Size: 312399
MIME Type: image/jpeg
Done

Different file sizes every time, I have never seen this before.

Any Ideas?

nosanity

7:57 am on Mar 14, 2003 (gmt 0)

10+ Year Member



Your binary files may be uploaded incorrectly if you use modules what recode characters. For example, for Russian Apache, you should use
<Files ScriptThatReceivesUploads.php>
CharsetDisable On
</Files>

If that is not the problem, try reading the file into a string, use stripslashes(), then re-write the file.

If all else fails, wait for someone else to post a better response. ;-)

noSanity

Allen

8:56 am on Mar 14, 2003 (gmt 0)

10+ Year Member



The php.net manual is your friend - taught me everything I needed to know to upload files :)

Allen

RussellC

2:22 pm on Mar 14, 2003 (gmt 0)

10+ Year Member



Thanks for the responses guys. I have tried the PHP manual and I also tried adding

<Files ScriptThatReceivesUploads.php>
CharsetDisable On
</Files>

to my httpd.conf and apache wouldn't even start when I did that.

Any other ideas? Thanks again

RussellC

4:10 pm on Mar 14, 2003 (gmt 0)

10+ Year Member



I also just tried CharsetRecodeMultipartForms Off but it broke the server as well. Am I not putting it in the correct place? I tired to put it in .htaccess and in httpd.conf and it messed apache up.

nosanity

4:41 pm on Mar 14, 2003 (gmt 0)

10+ Year Member



Did you try stripslashes()?

noSanity

RussellC

4:47 pm on Mar 14, 2003 (gmt 0)

10+ Year Member



yeah i set $userfile = stripslashes($userfile);
Still does the same thing :(

nosanity

4:54 pm on Mar 14, 2003 (gmt 0)

10+ Year Member



Alright, it's beyond me. Sorry.

noSanity

RussellC

9:20 pm on Mar 17, 2003 (gmt 0)

10+ Year Member



Sorry to bump this up again but I got somewhere today.. I uploaded a really small gif and it worked it was 599 bytes. Then I tried to upload a bigger jpg (7k) and it worked as well but it corrupted the image. The top of the image was ok, then it got all scrambled. Does this behavior give anyone any ideas? Any reason that it would be corrupting binary files?

Thanks

nosanity

12:51 am on Mar 18, 2003 (gmt 0)

10+ Year Member



Try this:

// Open, read, then close the temp file
$input_handler = fopen($userfile, "r");
$file_contents = fread($input_handler, strlen($file_contents));
fclose($input_handler);

// Clean up the string
$clean_file_contents = stripcslashes($file_contents);

// Write the file
$new_filename = "/var/www/html/filelistings".$userfile_name;
$output_handler = fopen($new_filename, "w");
fwrite($output_handler, $clean_file_contents, strlen($clean_file_contents));
fclose($output_handler);

------

Perhaps that might work.

noSanity

[edited by: nosanity at 1:01 am (utc) on Mar. 18, 2003]

nosanity

12:56 am on Mar 18, 2003 (gmt 0)

10+ Year Member



Also, I think I may have found a fix for your problem in an odd spot on PHP's site.

If you are on an apache server you can also alter this setting via the .htaccess file by creating a .htaccess file with the line

php_value magic_quotes_gpc 0

noSanity

RussellC

2:49 am on Mar 18, 2003 (gmt 0)

10+ Year Member



Thanks again nosanity, I'll try it in the morning.

Can you edit the .htacces to affect one file only?, i.e -

<Files example.php>
php_value magic_quotes_gpc 0
</Files>

nosanity

6:35 am on Mar 18, 2003 (gmt 0)

10+ Year Member



I am not sure if that directive can be used within a Files block or not. But as soon as you try you will post the results, right? ;-)

noSanity

RussellC

2:18 pm on Mar 18, 2003 (gmt 0)

10+ Year Member



Bummer, the no magic quotes dosn't work, and I tried to open the file, read it, and clean it up, but i get a 0 byte file when that happens. I appreciate the help.

Birdman

3:44 pm on Mar 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You may still need to define max file size in your form.

<form enctype="multipart/form-data" action="<?=$PHP_SELF?>" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="500000">
<input name="userfile" type="file" />
<input type="submit" />
</form>

RussellC

4:09 pm on Mar 18, 2003 (gmt 0)

10+ Year Member



Thanks Birdman, however I tried that as well. I have uploaded a 20MB txt file just fine, It's just any other MIME type file with a size larger than a few bytes gets corrupted. :(

nosanity

6:23 pm on Mar 18, 2003 (gmt 0)

10+ Year Member



At this point, I would

A) add PHP's General user group (news://news.php.net/php.general) to my favorite news reader, and post a message regarding this topic. Beware, this is a general user group, and it is quite busy. (That is why I didn't suggest going onto the mailing list).

-- OR --
B) Install the latest version of PHP and see if that fixes the problem by itself.

noSanity

RussellC

7:05 pm on Mar 18, 2003 (gmt 0)

10+ Year Member



Thanks again for the help. I am starting to think this is a Redhat 8/Apache2/PHP combo problem. I just got an e-mail from someone on a PHP forum with the same setup and he has the same problem. I'll let you know if I find a fix.

Thanks.

RussellC

7:20 pm on Mar 18, 2003 (gmt 0)

nosanity

8:17 pm on Mar 18, 2003 (gmt 0)

10+ Year Member



So have you tried changing your Apache2 configuration

FROM:

<Files *.php>
SetOutputFilter PHP
SetInputFilter PHP
LimitRequestBody 524288
</Files>

TO:

AddType application/x-httpd-php .php

-- OR

Tried the latest RC from PHP?
[qa.php.net ]

noSanity

RussellC

8:22 pm on Mar 18, 2003 (gmt 0)

10+ Year Member



YES! I just tried that right before you posted and that fixed it finally, all I did was comment out the lines:

SetOutputFilter PHP
SetInputFilter PHP

What a fiasco! Thanks for your help nosanity!

Alternative Future

8:30 pm on Mar 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

Sorry to intrude, but I was looking over this discussion and had another thought on what might be the problem (forgive me if I am wrong). Could it not be your FTP program that is at fault, some FTP clients try to upload images in ASCII and that gives back errors alike to what has been suggested.

Just a thought :)

-gs

nosanity

12:04 am on Mar 19, 2003 (gmt 0)

10+ Year Member



He isn't using FTP. He is using an HTML form to upload a file to the server.

noSanity