title: php force download pdf from a dynamic link and store to local folder
body: <p>I am building a script that imap's into a google mailbox, gets a message, and extracts a link from the body. The link, when opened via php, starts a download process of a PDF file from an external source.</p>
  
  <p>Here is what I need to do:</p>
  
  <ol>
  <li>Programmatically open the link which initiates a download</li>
  <li>Download the PDF, bypassing any dialog boxes obviously</li>
  <li>Store the PDF file to a local folder on the web server</li>
  </ol>
  
  <p>I have been successful up to the point where I can initiate the download process and download the file. However, every attempt at opening the PDF finds the file to be corrupted and I can't figure out why. Here's what I'm trying now. The following is based on similar topics.</p>
  
  <pre><code>    $filename =  http://cckk.ca/KE645R26/geico.com_SEO_Domain_Dashboard_20121101_00.pdf
  
      header('Content-Description: File Transfer');
      header('Content-Type: application/pdf');
      header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
      header('Content-Transfer-Encoding: binary');
      header('Expires: 0');
      header('Pragma: public');
      header('Content-Length: ' . filesize($filename));
      ob_clean();
      flush();
      readfile($filename);
      exit;
  </code></pre>
  
  <p>I am thinking that maybe there's an issue with the filename I'm passing?</p>
  
  <p><strong>Note:</strong> This script will be setup as a CRON task that is run on the server every few seconds, in order to constantly fetch from OUR own mailbox, so this is not going to have any type of user interaction or be a security risk to a user.</p>