HTTP POST file uploads incomplete, using fsockopen
Posted: Sun Nov 28, 2004 7:43 pm
I am working on php code (below) to post an image file that it receives from a from upload to an other script to be processed there.
The Post request is more or less working, no error message, the other form data variables are sent and interpreted correctly by the target script.
But the problem is:
The file upload of the image is INCOMPLETE! I upload for example a 9kb file and only got a 3kb file on the server!
When I look at the POST request (the commented out line in the code...), I see the binary content of the image file, so my question is: why doesn't the server receive the whole file?
The script on the target server script works perfectly, that can't be the cause of the problem. I tested this with direct upload from a form.
$_REQUEST['imgfile']['size'] contains the correct filesize. But Content-Length contains the wrong size. How can that be? And how can I work around this and calculate the correct size of the $data string?
The Post request is more or less working, no error message, the other form data variables are sent and interpreted correctly by the target script.
But the problem is:
The file upload of the image is INCOMPLETE! I upload for example a 9kb file and only got a 3kb file on the server!
When I look at the POST request (the commented out line in the code...), I see the binary content of the image file, so my question is: why doesn't the server receive the whole file?
The script on the target server script works perfectly, that can't be the cause of the problem. I tested this with direct upload from a form.
$_REQUEST['imgfile']['size'] contains the correct filesize. But Content-Length contains the wrong size. How can that be? And how can I work around this and calculate the correct size of the $data string?
Code: Select all
$host='sublx.unibe.ch'; $port=80;
$path="/cgi-bin/tor.cgi";
$fn = $_REQUEST['imgfile']['tmp_name'];
$fna = $_REQUEST['imgfile']['name'];
$content_type = "image/jpeg"; // the file mime type
$boundary = "GATTACA";
$content_file = join("", file($fn));
$data = "--$boundary\r\n";
foreach($_REQUEST as $key=>$value) {
if (count($value)==1) {
$data.="Content-Disposition: form-data; name="$key"\r\n\r\n";
$data.="$value\r\n--$boundary\r\n";
}
}
$data.="Content-Disposition: form-data; name="imgfile"; filename="$fna"\r\n";
$data.="Content-Type: $content_type\r\n\r\n$content_file\r\n--$boundary--\r\n\r\n";
$lenga=strlen($data);
$msg ="POST $path HTTP/1.0\r\n";
$msg.="Content-Type: multipart/form-data; boundary=$boundary\r\n";
$msg.="Content-Length: $lenga\r\n\r\n";
$f = fsockopen($host, $port);
//print "<pre>$msg$data</pre>";
fputs($f,$msg.$data);
while(!feof($f)) {
$buf = fgets($f, 128);
if ($ok) {$result .= $buf;}
if (preg_match("/^Content-Type/",$buf)) {$ok=1;}
}
fclose($f);return $result;