title: ftp_get(): Opening BINARY mode data connection when downloading a large file
body: <p>I am trying to download two files from two different FTP servers. One file is 10 MB in size while the other is 3.3GB in size. The 10MB file is downloaded every time without a problem. The 3.3GB file always encounters an error:</p>
  
  <blockquote>
    <p>Code Error: [2] ftp_get(): Opening BINARY mode data connection for
    bigfile.gz (3232089332 bytes).<br>Error/Warning on line 55 in file
    script.php<br><br>Please go over the collector code.</p>
  </blockquote>
  
  <p>The size of the file is exactly 3232089332, so this issue appears after the file has finished downloading completely.</p>
  
  <p>Both files are .gz files (so I know they are binary).</p>
  
  <p>There is enough space on the hard drive (currently free 47GB).</p>
  
  <p>It is worthwhile to note that I am able to download the file without any issues using Filezilla.</p>
  
  <p>Any help would be highly appreciated.</p>
  
  <p>The code is as follows:</p>
  
  <pre><code>function ftpDownload($server, $username, $password, $filename) {
  
      if (strpos($server, '://') !== false) $server = substr($server, strpos($server, '://') + 3);
  
      # set up basic connection
  
      echo "Connecting to $server\n";
      $connectionId = ftp_connect($server);
  
      if (!$connectionId) {
          return ['success' =&gt; false, 'error' =&gt; "FTP Connection has Failed"];
      }
  
      # login with username and password
  
      echo "Logging in\n";
  
      $loginResult = ftp_login($connectionId, $username, $password); 
      ftp_pasv($connectionId, true);
  
      # check connection
  
      if (!$loginResult) { 
          return ['success' =&gt; false, 'error' =&gt; "Failed to login"];
      }
  
      # Verify the file exists 
  
      echo "Locating $filename\n";
      $result = ftp_size($connectionId, $filename);
  
      if ($result == -1) {
          return ['success' =&gt; false, 'error' =&gt; "Unable to locate $filename in server", 'filename' =&gt; false];
      }
  
      echo "File size ".number_format(($result / 1024 / 1024))." MBs\n";
  
      # Download the file
  
      echo "Downloading $filename, this may take a while\n";
      if (file_exists(__DIR__.'/files/'.$filename)) unlink(__DIR__.'/files/'.$filename);
      ftp_pasv($connectionId, true);
      $success = ftp_get($connectionId, __DIR__.'/files/'.$filename, $filename, FTP_BINARY);
      ftp_close($connectionId);
  
      if ($success == false) {
          return ['success' =&gt; false, 'error' =&gt; "Failed to download file $filename from server, received error from FTP"];
      }
  
      if (!file_exists(__DIR__.'/files/'.$filename)) {
          return ['success' =&gt; false, 'error' =&gt; "Unable to locate $filename in server, filename was not properly stored locally"];
      }
  }
  </code></pre>