title: Stream FTP download to output
body: <p>I am trying to stream/pipe a file to the user's browser through HTTP from FTP.  That is, I am trying to print the contents of a file on an FTP server.</p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>public function echo_contents() {                    
      $file = fopen('php://output', 'w+');             
  
      if(!$file) {                                     
          throw new Exception('Unable to open output');
      }                                                
  
      try {                                            
          $this-&gt;ftp-&gt;get($this-&gt;path, $file);         
      } catch(Exception $e) {                          
          fclose($file);  // wtb finally               
  
          throw $e;                                    
      }                                                
  
      fclose($file);                                   
  }                                                    
  </code></pre>
  
  <p><code>$this-&gt;ftp-&gt;get</code> looks like this:</p>
  
  <pre><code>public function get($path, $stream) {
      ftp_fget($this-&gt;ftp, $stream, $path, FTP_BINARY);  // Line 200
  }
  </code></pre>
  
  <p>With this approach, I am only able to send small files to the user's browser.  For larger files, nothing gets printed and I get a fatal error (readable from Apache logs):</p>
  
  <blockquote>
    <p>PHP Fatal error:  Allowed memory size of 16777216 bytes exhausted (tried to allocate 15994881 bytes) in /xxx/ftpconnection.php on line 200</p>
  </blockquote>
  
  <p>I tried replacing <code>php://output</code> with <code>php://stdout</code> without success (nothing seems to be sent to the browser).</p>
  
  <p>How can I efficiently download from FTP while sending that data to the browser at the same time?</p>
  
  <p><em>Note: I would not like to use <code>file_get_contents('ftp://user:pass@host:port/path/to/file');</code> or similar.</em></p>