title: Remotely download a file from an external link to my server - download stops prematurely
body: <p>I have a page set where I enter the url of an file and I have my server download that file and save it into a folder on my server. The issue is that I dont know how to download a file to my server. I have tried both of the following methods, and they both didn't work.</p>
  
  <p>This one throws an error about a failed open stream:</p>
  
  <pre><code>$url = 'http://www.example.com/file.zip';
  $enc = urlencode($url);
  $dir = "/downloads/file.zip";
  $raw = file_get_contents($enc);
  file_put_contents($dir, $raw);
  </code></pre>
  
  <p>This one works but I only get 18kb out of a 270kb file: ( I have tried to increase the timeout)</p>
  
  <pre><code>set_time_limit(0);
  
  $url = 'http://www.eample.com/file.zip';
  $fp = fopen ('/downloads/file.zip', 'w+');
  
      $ch = curl_init($url);
  
      curl_setopt_array($ch, array(
      CURLOPT_URL            =&gt; $url,
      CURLOPT_BINARYTRANSFER =&gt; 1,
      CURLOPT_RETURNTRANSFER =&gt; 1,
      CURLOPT_FILE           =&gt; $fp,
      CURLOPT_TIMEOUT        =&gt; 50,
      CURLOPT_USERAGENT      =&gt; 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)'
      ));
  
  $results = curl_exec($ch);
  if(curl_exec($ch) === false)
   {
    echo 'Curl error: ' . curl_error($ch);
   }
  </code></pre>