title: Stream ftp file to browser
body: <p>I am trying to retrieve and stream a file to the browser from an ftp site.</p>
  
  <p>The error is:</p>
  
  <blockquote>
    <p>Warning: ftp_nb_get() expects parameter 2 to be string, resource given</p>
  </blockquote>
  
  <p>I know that it is a resource but how do I get around this?</p>
  
  <pre><code>if(isset($_GET['filename'])){
      $requestfilename = $_GET['filename'];
  
      if($sockets = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP)){
          stream_set_write_buffer($sockets[0], 0);
          stream_set_timeout($sockets[1], 0);
          if($ftp_connection = ftp_connect($ftp_server)){
              if (@ftp_login($ftp_connection, $ftp_user, $ftp_pass)) {
                  if(@ftp_chdir($ftp_connection, $ftp_dir_new)){
                      if($ret = ftp_nb_get($ftp_connection, $sockets[0], $requestfilename, FTP_BINARY)){
                          while(ftp_nb_continue($ftp_connection)==FTP_MOREDATA){
                              $contents = stream_get_contents($sockets[1]);
                              if($contents !== false) {
                                  echo $contents;
                                  flush();
                              }
                          }
                          if ($ret != FTP_FINISHED) $error[] = 'There was an error downloading the file...';
                      }else{
                      $error[] = "Could not ftp_nb_get file from $ftp_server";
                      }
                  }else{
                      $error[] = "Couldn't cd to $ftp_dir_new";
                  }
              } else {
                  $error[] = "Couldn't connect as $ftp_user";
              }
          }else{
              $error[] =  "Couldn't connect to $ftp_server";
          }
      }else{
          $error[] = "Unable to create socket pair";
      }
  
      if($sockets){
          fclose($sockets[0]);
          fclose($sockets[1]);
      }
      if( ! empty($ftp_connection)) ftp_close($ftp_connection);
  }
  </code></pre>