title: How to tell if curl download is complete using CURLOPT_FILE in PHP
body: <p>I have a function that downloads a specific zip file from a remote server to my local Windows server. The file ranges in size from 1-10 mb and I want my script to wait until it's complete. How can I tell with certainty that it's done? The actual downloading works fine.</p>
  
  <p>The function is:</p>
  
  <pre><code>function get_download($url, $path) 
  { 
      $curl = curl_init(); 
      curl_setopt($curl, CURLOPT_URL,$url); 
      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); 
      curl_setopt($curl, CURLOPT_RETURNTRANSFER,1); 
      curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie2.txt'); 
      curl_setopt($curl, CURLOPT_FILE, $path);
      $results = curl_exec($curl); 
      return $results; 
  }
  </code></pre>
  
  <p>I thought I would get the size of the remote file first then compare to the local file size. However, this doesn't seem to work. Not sure if its a function of the file being a .zip file but it always returns -1 or -11. I tried this function:</p>
  
  <pre><code>function remotefileSize($url) 
  {
      $curl = curl_init($url);
      curl_setopt($curl, CURLOPT_NOBODY, 1);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, 0);
      curl_setopt($curl, CURLOPT_HEADER, 0);
      curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
      curl_setopt($curl, CURLOPT_MAXREDIRS, 3);
      curl_exec($curl);
      $filesize = curl_getinfo($curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
      curl_close($curl);
      return $filesize;
  }
  </code></pre>