From: <php...@li...> - 2010-02-07 08:26:04
|
> If the data you receive is encoded in HTTP/1.1 chunks I don't believe it is, but I don't know a lot about these things, so I might be wrong. There's nothing special happening at the server side. It's just a GET request and response. I'm just trying to download the response in a way that doesn't cause me memory problems with files that can be 100MBs in size. At present, the code below is producing an adequate result, ie: it's not failed in testing yet. The downloads only fail 'sometimes', so I don't understand how some of the explanations offer here fit to what's happening. The retry improves the chance of the downloading completing. If a file does not complete after the retries, the client handles this, and the downloads are retried again later. Thanks thanks for everyone's advice though. Some of it is over my head as a PHP newbie, but I'm understanding and learning other parts of what is being said. Cheers, Mark... $download_attempt = 1; do { $fs = fsockopen($host, 80, $errno, $errstr, 30); if (!$fs) { $this->writeDebugInfo("FAILED ", $errstr . '(' . $errno . ')'); } else { $out = "GET $file HTTP/1.1\r\n"; $out .= "Host: $host\r\n"; $out .= "Connection: Close\r\n\r\n"; fwrite($fs, $out); $fm = fopen ($temp_file_name, "w"); stream_set_timeout($fs, 30); while(!feof($fs) && ($debug = fgets($fs)) != "\r\n" ); // ignore headers while(!feof($fs)) { $contents = fgets($fs, 4096); // Chunk download fwrite($fm, $contents); $info = stream_get_meta_data($fs); if ($info['timed_out']) { break; } } fclose($fm); fclose($fs); if ($info['timed_out']) { // Delete temp file if fails unlink($temp_file_name); $this->writeDebugInfo("FAILED on attempt " . $download_attempt . " - Connection timed out: ", $temp_file_name); $download_attempt++; if ($download_attempt < 5) { $this->writeDebugInfo("RETRYING: ", $temp_file_name); } } else { // Move temp file if succeeds $media_file_name = str_replace('temp/', 'media/', $temp_file_name); rename($temp_file_name, $media_file_name); $this->writeDebugInfo("SUCCESS: ", $media_file_name); } } } while ($download_attempt < 5 && $info['timed_out']); |