Welcome to WebmasterWorld Guest from 18.232.124.77

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP file uploading problems still

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

Preferred Member

10+ Year Member

joined:Jan 8, 2002
posts:465
votes: 0


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?

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

Junior Member

10+ Year Member

joined:Dec 6, 2001
posts:99
votes: 0


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

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

Junior Member

10+ Year Member

joined:Jan 14, 2002
posts:118
votes: 0


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

Allen

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

Preferred Member

10+ Year Member

joined:Jan 8, 2002
posts:465
votes: 0


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

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

Preferred Member

10+ Year Member

joined:Jan 8, 2002
posts:465
votes: 0


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.
4:41 pm on Mar 14, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Dec 6, 2001
posts:99
votes: 0


Did you try stripslashes()?

noSanity

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

Preferred Member

10+ Year Member

joined:Jan 8, 2002
posts:465
votes: 0


yeah i set $userfile = stripslashes($userfile);
Still does the same thing :(
4:54 pm on Mar 14, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Dec 6, 2001
posts:99
votes: 0


Alright, it's beyond me. Sorry.

noSanity

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

Preferred Member

10+ Year Member

joined:Jan 8, 2002
posts:465
votes: 0


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

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

Junior Member

10+ Year Member

joined:Dec 6, 2001
posts:99
votes: 0


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]

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

Junior Member

10+ Year Member

joined:Dec 6, 2001
posts:99
votes: 0


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

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

Preferred Member

10+ Year Member

joined:Jan 8, 2002
posts:465
votes: 0


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>

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

Junior Member

10+ Year Member

joined:Dec 6, 2001
posts:99
votes: 0


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

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

Preferred Member

10+ Year Member

joined:Jan 8, 2002
posts:465
votes: 0


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.
3:44 pm on Mar 18, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 22, 2002
posts:2546
votes: 0


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>

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

Preferred Member

10+ Year Member

joined:Jan 8, 2002
posts:465
votes: 0


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. :(
6:23 pm on Mar 18, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Dec 6, 2001
posts:99
votes: 0


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

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

Preferred Member

10+ Year Member

joined:Jan 8, 2002
posts:465
votes: 0


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.

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

Preferred Member

10+ Year Member

joined:Jan 8, 2002
posts:465
votes: 0

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

Junior Member

10+ Year Member

joined:Dec 6, 2001
posts:99
votes: 0


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

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

Preferred Member

10+ Year Member

joined:Jan 8, 2002
posts:465
votes: 0


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!

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 22, 2003
posts:1023
votes: 0


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

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

Junior Member

10+ Year Member

joined:Dec 6, 2001
posts:99
votes: 0


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

noSanity

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members