PHP - Downloading very large files with fsockopen(), fgets() and feof()
<p>I have a simple download function in a class that might be dealing with files of many hundreds of megabytes at a time from an Amazon Web Services bucket. The whole file cannot be loaded into memory at once, so it must be streamed directly to a file pointer. This is my understanding as this is the first time I've dealt with this issue and I'm picking things up as I go along.</p>

<p>I've ended up with this, based on a 4 KB file buffer which simple testing showed was a good size:</p>

<pre><code>        $fs = fsockopen($host, 80, $errno, $errstr, 30);

        if (!$fs) {
          $this-&gt;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) &amp;&amp; ($debug = fgets($fs)) != "\r\n" ); // ignore headers

          while(!feof($fs)) {
            $contents = fgets($fs, 4096); 
            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-&gt;writeDebugInfo("FAILED - Connection timed out: ", $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-&gt;writeDebugInfo("SUCCESS: ", $media_file_name);
          }
        }
</code></pre>

<p>In testing it's fine. However I have got into a conversation with someone who is saying that I am not understanding how <code>fgets()</code> and <code>feof()</code> work together, and he's mentioning chunked encoding as a more efficient method.</p>

<p>Is the code generally OK, or am I missing something vital here? What is the benefit that chunked encoding will give me?</p>
