title: Serve file to user over http via php
body: <p>If I goto <a href="http://site.com/uploads/file.pdf" rel="nofollow noreferrer">http://site.com/uploads/file.pdf</a> I can retrieve a file.  </p>
  
  <p>However, if I have a script such as: </p>
  
  <pre><code>&lt;?php 
  
  ini_set('display_errors',1);
  error_reporting(E_ALL|E_STRICT);
  
  //require global definitions 
  require_once("includes/globals.php"); 
  //validate the user before continuing 
  isValidUser(); 
  $subTitle = "Attachment";   
  $attachmentPath = "/var/www/html/DEVELOPMENT/serviceNow/selfService/uploads/";
  if(isset($_GET['id']) and !empty($_GET['id'])){
      //first lookup attachment meta information 
      $a = new Attachment(); 
      $attachment = $a-&gt;get($_GET['id']); 
      //filename will be original file name with user name.n prepended 
      $fileName = $attachmentPath.$_SESSION['nameN'].'-'.$attachment-&gt;file_name; 
      //instantiate new attachmentDownload and query for attachment chunks 
      $a = new AttachmentDownload(); 
      $chunks= $a-&gt;getRecords(array('sys_attachment'=&gt;$_GET['id'], '__order_by'=&gt;'position')); 
  
  
      $fh = fopen($fileName.'.gz','w');                                                      
      // read and base64 encode file contents 
      foreach($chunks as $chunk){
              fwrite($fh, base64_decode($chunk-&gt;data));   
      }
      fclose($fh);
  
      //open up filename for writing 
      $fh = fopen($fileName,'w');     
      //open up filename.gz for extraction                                
      $zd = gzopen($fileName.'.gz', "r");
      //iterate over file and write contents 
      while (!feof($zd)) {
              fwrite($fh, gzread($zd, 60*57));    
      }
      fclose($fh); 
      gzclose($zd);
      unlink($fileName.'.gz'); 
      $info = pathinfo($fileName); 
  
      header('Content-Description: File Transfer');
      header('Content-Type: '.Mimetypes::get($info['extension']));
      header('Content-Disposition: attachment; filename=' . basename($fileName));
      header('Content-Transfer-Encoding: binary');
      header('Expires: 0');
      header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
      header('Pragma: public');
      header('Content-Length: ' . filesize($fileName));
      ob_clean();
      flush();
      readfile($fileName);
      exit();
  }else{
      header("location: ".$links['status']."?".urlencode("item=incident&amp;action=view&amp;status=-1&amp;place=".$links['home']));   
  }
  
  
  ?&gt;
  </code></pre>
  
  <p>This results in sending me the file, but when I open it I receive an error saying:</p>
  
  <pre><code>"File type plain text document (text/plain) is not supported"
  </code></pre>