title: PHP server to server file request
body: <p>I have script-1 on server A, where user ask for a file.<br>
  I have script-2 on server B (the file repository) where I check that user can access it and return the correct file (I'm using Smart File Download <a href="http://www.zubrag.com/scripts/download.php" rel="nofollow">http://www.zubrag.com/scripts/download.php</a>).  </p>
  
  <p>I've tried cURL and file_get_contents, I've changed Content Header in various ways, but I wasn't still able to download the file.</p>
  
  <p>This is my request:</p>
  
  <pre><code>$request = "http://mysite.com/download.php?f=test.pdf";
  </code></pre>
  
  <p>and it works fine.
  What should I call in script-1 to force the file be downloaded?</p>
  
  <p><strong>Some of my tries</strong></p>
  
  <p>This works, but I don't know how to handle unauthorized or broken downloads</p>
  
  <pre><code>header('Content-type: application/pdf');
  
  $handle = fopen($request, "r");
  
  if ($handle) {
      while (!feof($handle)) {
          $buffer = fgets($handle, 4096);
          echo $buffer;
      }
      fclose($handle);
  }
  </code></pre>
  
  <p>This prints the pdf code (not the text) straight in the browser (I think it's a header problem):</p>
  
  <pre><code>$c = curl_init();
  curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($c, CURLOPT_URL, $request);
  $contents = curl_exec($c);
  curl_close($c);
  
  if ($contents) return $contents;
      else return FALSE;
  </code></pre>
  
  <p>This generate a white page</p>
  
  <pre><code>file_get_contents($request);
  </code></pre>