title: Download external file with PHP fopen and/or cURL
body: <p>I can't get my download script to work with external files, the file will download but is corrupted/not working. I think it's because I can't get the filesize of the external file with filesize() function.</p>
  
  <p>This is my script:</p>
  
  <pre><code>function getMimeType($filename){
      $ext = pathinfo($filename, PATHINFO_EXTENSION);
      $ext = strtolower($ext);
  
      $mime_types=array(
          "pdf" =&gt; "application/pdf",
          "txt" =&gt; "text/plain",
          "html" =&gt; "text/html",
          "htm" =&gt; "text/html",
          "exe" =&gt; "application/octet-stream",
          "zip" =&gt; "application/zip",
          "doc" =&gt; "application/msword",
          "xls" =&gt; "application/vnd.ms-excel",
          "ppt" =&gt; "application/vnd.ms-powerpoint",
          "gif" =&gt; "image/gif",
          "png" =&gt; "image/png",
          "jpeg"=&gt; "image/jpg",
          "jpg" =&gt;  "image/jpg",
          "php" =&gt; "text/plain",
          "csv" =&gt; "text/csv",
          "xlsx" =&gt; "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
          "pptx" =&gt; "application/vnd.openxmlformats-officedocument.presentationml.presentation",
          "docx" =&gt; "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
      );
  
      if(isset($mime_types[$ext])){
          return $mime_types[$ext];
      } else {
          return 'application/octet-stream';
      }
  }
  
  $path = "http://www.example.com/file.zip";
  
  /* Does not work on external files
  // check file is readable or not exists
  if (!is_readable($path))
      die('File is not readable or does not exists!');
  */
  
  $file_headers = @get_headers($path);
  if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
      echo "Files does not exist.";
  } else {
  
  $filename = pathinfo($path, PATHINFO_BASENAME);
  
  // get mime type of file by extension
  $mime_type = getMimeType($filename);
  
  // set headers
  header('Pragma: public');
  header('Expires: -1');
  header('Cache-Control: public, must-revalidate, post-check=0, pre-check=0');
  header('Content-Transfer-Encoding: binary');
  header("Content-Disposition: attachment; filename=\"$filename\"");
  header("Content-Length: " . filesize($path));
  header("Content-Type: $mime_type");
  header("Content-Description: File Transfer");
  
  // read file as chunk
  if ( $fp = fopen($path, 'rb') ) {
      ob_end_clean();
  
      while( !feof($fp) and (connection_status()==0) ) {
          print(fread($fp, 8192));
          flush();
      }
  
      @fclose($fp);
      exit;
  }
  
  }
  </code></pre>
  
  <p>I believe it can be done with cURL - but my knowledge is lacking. 
  What I would like to know:</p>
  
  <ul>
  <li><p>How do I check if the file exist and how do I get the filesize with cURL?</p></li>
  <li><p>Would it be better just to use cURL and forget about fopen?</p></li>
  <li><p>Is the headers set correctly?</p></li>
  </ul>
  
  <p>Any advice is much appreciated!</p>