views:

222

answers:

3

Hi all,

I'm working with the following section of php code. The purpose is to POST an image file from one server to another. This works perfectly and very quickly as long as the file is < ~250k. Much over 250k, and the process time jumps to ~60 seconds, and increases from there with image size.

Any ideas?

Thanks!

John

$fileContents = file_get_contents($_FILES[$key]['tmp_name']);
$time = strtotime("now");
$body = "--f6sd54c2
Content-Disposition: form-data; name=\"id\"
Content-Length: " . strlen($id) . "

{$id}
--f6sd54c2
Content-Disposition: form-data; name=\"key\"
Content-Length: " . strlen("654t2bsr65t42czd13fvs6dry87") . "

654t2bsr65t42czd13fvs6dry87
--f6sd54c2
Content-Disposition: form-data; name=\"time\"
Content-Length: " . strlen($time) . "

{$time}
--f6sd54c2
Content-Disposition: form-data; name=\"photo\"; filename=\"photo.jpg\"
Content-Type: image/jpeg
Content-Length: " . strlen($fileContents) . "

{$fileContents}
--f6sd54c2--";


$headers = "POST /photos/process.php HTTP/1.1
Host: www.hostname.com
Keep-Alive: 300
Connection: keep-alive
Content-Type: multipart/form-data; boundary=f6sd54c2
Content-Length: " . strlen($body) . "

";

$fp = fsockopen("www.hostname.com", 80, $errno, $errstr);
if ($fp){
    fwrite($fp, $headers . $body);
    fclose($fp);
}
+2  A: 

You are using HTTP/1.1 even though you are closing the connection right away. HTTP/1.1 maintains the connection which maybe introducing the lag. You should have used CURL, writing your own client is always a bad idea, especially for HTTP. Engineers should never reinvent the wheal. If changing your header to HTTP/1.0 doesn't work, curl will fix the problem.

Rook
A: 

You also have Connection: keep-alive.. this will keep the connection open. Try connection:close to end the connection after the request is made

Phil
A: 

Since I originally posted without registering, I'm not sure I can reply/edit my post correctly, but I will respond here...

  • The server connection is simply through the net.

  • It would certainly make my life much easier to use CURL, but sadly it is not available on this server.

  • Tried switching to HTTP/1.0, tried connection: close, but the problem remains the same. Near instant transfer below a certain level (few hundred kb), anywhere above that level and it takes at least one minute.

Thanks.

JohnS