title: Collecting file with PHP CURL after validating request downloads an empty file
body: <p>I am doing a system where one of my sites goes to the other to get documents.
  On the first site I am using Curl to make a request to get the file wanted:
  I am using the solution from <a href="https://stackoverflow.com/questions/13168198/download-file-from-url-using-curl">Download file from URL using CURL</a> :</p>
  
  <pre><code>function collect_file($url){
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL, $url);
          curl_setopt($ch, CURLOPT_VERBOSE, 1);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt($ch, CURLOPT_AUTOREFERER, false);
          curl_setopt($ch, CURLOPT_REFERER, "http://example.com");
          curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
          curl_setopt($ch, CURLOPT_HEADER, 0);
          $result = curl_exec($ch);
          curl_close($ch);
          echo $result;
          return($result);
      }
  
      function write_to_file($text,$new_filename){
          $fp = fopen($new_filename, 'w');
          fwrite($fp, $text);
          fclose($fp);
      }
  
      $curlUrl = 'http://site2.com/file-depository/14R4NP8JkoIHwIyjnexSUmyJibdpHs5ZpFs3NLFCxcs54kNhHj';
  
              $new_file_name = "testfile-new.png";
              $temp_file_contents = collect_file($curlUrl);
              write_to_file($temp_file_contents,$new_file_name);
  </code></pre>
  
  <p>I am testing downloading an image. If i use a direct URL into $curlUrl , for instance <a href="http://site2.com/file-depository/image.png" rel="nofollow noreferrer">http://site2.com/file-depository/image.png</a> it works perfect.</p>
  
  <p>What I am doing is that the URL <a href="http://site2.com/file-depository/14R4NP8JkoIHwIyjnexSUmyJibdpHs5ZpFs3NLFCxcs54kNhHj" rel="nofollow noreferrer">http://site2.com/file-depository/14R4NP8JkoIHwIyjnexSUmyJibdpHs5ZpFs3NLFCxcs54kNhHj</a> is then parsed and checked against a database to match the document requested, once there is a document matched I need to provide this document to the Curl response.
  I have tried many ways to read the file but everytime i am getting a file on the other end but it is only 1kb in size (45 expected) and when trying to open it i get an error unkown file type etc.</p>
  
  <p>On the second site, once the URL is validated here is what I have:</p>
  
  <pre><code>$file = readfile('some-image.png');
  echo $file;
  </code></pre>
  
  <p>I am guessing there is part of the information which belongs to the file missing but can't figure it out, any pointers appreciated!</p>