note 11793 deleted from features.file-upload by jimw
| From: | jimw@php.net | Date: | Wed, 02 Jan 2002 00:23:08 +0000 |
| Subject: | note 11793 deleted from features.file-upload by jimw | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-24050@lists.php.net to get a copy of this message | ||
There does indeed seem to be a bug in file uploads on my version of PHP4.04pl1 on RedHat. Up to two
extra lines are prepended to any file uploaded depending on the browser it originated from. Uploads
from Internet Explorer will always have a 'Content-type:' line followed by one blank line,
but uploads from Netscape may have these two lines or just one blank line (containing a \r\n
sequence). This function should fix this problem and return the correct file size in bytes also:
function fix_php_upload_bug($tmp) {
$infile=fopen($tmp,"r"); // Open the file for the copy
$outfile=fopen("$tmp.new","w"); // create a new temp file
$header=fgets($infile,255); //get the 1st line (netscape sometimes doesn't add a
Content-type line)
//if its more than just a \r\n sequence then
if (strlen($header)>2) $header=fgets($infile,255); //get next line also
while(!feof($infile)) { // Loop through the remaining file
$temp=fread($infile,128);
fwrite($outfile,$temp,strlen($temp)); //copying contents to new temp file
}
fclose($outfile);
fclose($infile);
copy("$tmp.new","$tmp"); //replace the original with our new bug fixed file
unlink("$tmp.new"); //and delete the new file
return filesize($tmp); //return a true file size
}
I also noticed that other POST variables included with the file (my script used several hidden input
fields within the same form to describe the location my file was to be saved) were corrupted with an
extra \r\n sequence prepended to them.
I had to use code like this to put them right:
$xtra=substr($xtra,2,999); //strip leading carriage return/newline
Hope this helps others make file uploads work for this version. Can anyone tell me if other versions
are also affected this way so that I can make this fix version dependant in my code?