title: SFTP file is not downloaded completelly with ssh2.sftp and fread
body: <p>I am working on a log file download for a separate system that requires SFTP for viewing logs. I am able to view the available log files on the server and download them. My issue is that it seems that the download stops at 2K which, in most cases, is only the first 10 lines of the log file. The files should contain thousands of lines as they are daily logs of changes made to the system.</p>
  
  <p>I have done this in two separate files, one loads all of the files onto a page where the user can select the available log files and click a link to view the contents in the browser:</p>
  
  <pre><code>$ftp_server = "IP of Server";
  $ftp_user_name = "USERNAME";
  $ftp_user_pass = "PASSWORD";
  
  $connection = ssh2_connect($ftp_server, 22); 
  ssh2_auth_password($connection,$ftp_user_name, $ftp_user_pass);
  $sftp = ssh2_sftp($connection); 
  
  $dh = opendir("ssh2.sftp://$sftp//EMSftp/audit_files/"); 
  
  while (($file = readdir($dh)) !== false) { 
      if ($file != '.' &amp;&amp; $file != '..'){
          if (strpos($file,"2017-04")){
              echo '/EMSftp/audit_files/'.$file.' | &lt;a href="open_file_curl.php?file='.$file.'" target="_blank"&gt;curl&lt;/a&gt;
                                                  | &lt;a href="open_file.php?file='.$file.'" target="_blank"&gt;view&lt;/a&gt;&lt;br&gt;';
          }
      }
  } 
  
  closedir($dh);
  </code></pre>
  
  <p>I have tried to download the files two different ways using sftp and ssh2:</p>
  
  <pre><code>$file = $_GET['file'];
  $local_file = "/var/www/uploads/logs/$file";
  
  if (!file_exists($local_file)){
      $connection = ssh2_connect($ftp_server, 22); 
      ssh2_auth_password($connection,$ftp_user_name, $ftp_user_pass);
      $sftp = ssh2_sftp($connection); 
  
      $stream = @fopen("ssh2.sftp://$sftp//EMSftp/audit_files/$file", 'r');
  
      if (! $stream)
          throw new Exception("Could not open file: $remote_file");
      $contents = fread($stream, filesize("ssh2.sftp://$sftp//EMSftp/audit_files/$file"));            
      file_put_contents ($local_file, $contents);
      @fclose($stream);
  }
  
  echo '&lt;pre&gt;';
  echo file_get_contents($local_file);
  echo '&lt;/pre&gt;';
  </code></pre>
  
  <p>and also tried to accomplish this using curl. This only creates a blank file. Not sure what is missing here. Unable to add the file contents to the file.</p>
  
  <pre><code>$file = $_GET['file'];
  
  $local_file = "/var/www/uploads/logs/$file";
  
  fopen($local_file, 'w+');
  chmod($local_file, 0777);
  
  $remote = "sftp://$ftp_user_name:$ftp_user_pass@$ftp_server//EMSftp/audit_files/$file";
  
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL, $remote);
  curl_setopt($curl, CURLOPT_PROTOCOLS, CURLPROTO_SFTP);
  curl_setopt($curl, CURLOPT_USERPWD, "$ftp_user_name:$ftp_user_pass");
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  $somecontent = curl_exec($curl);
  if (is_writable($local_file)) {
  
      if (!$handle = fopen($local_file, 'a')) {
           echo "Cannot open file ($local_file)";
           exit;
      }
  
      if (fwrite($handle, $somecontent) === FALSE) {
          echo "Cannot write to file ($local_file)";
          exit;
      }
  
      echo "Success, wrote ($somecontent) to file ($local_file)";
  
      fclose($handle);
  
  } else {
      echo "The file $local_file is not writable";
  }
  curl_close($curl);
  </code></pre>
  
  <p>Not sure where I am missing something. Wondering if there is a time out related to this procedure that I am overlooking. Any help?</p>