title: PHP file download gives garbage for anything BUT .pdf files
body: <p>I am having an issue in which the only filetype capable of being downloaded correctly from the server is ".pdf".  Other files of other file types exist in the same location, upload perfectly fine, have the correct permissions, and load just fine when I scp them off to my local machine.  I can't show the entirety of the code involved since this is paid software, but honestly it seems that the problem shouldn't even lie in the code itself...</p>
  
  <p>I've tried re-writing the first function below with something similar to this: <a href="http://www.finalwebsites.com/forums/topic/php-file-download" rel="nofollow">http://www.finalwebsites.com/forums/topic/php-file-download</a> and then simply feeding it the correct values to make it pick specific files from the server.  The files downloaded the same either way.  They would show up as the correct file type with the correct name and even the correct size, but they would not open as anything but garbage values (unless a PDF).  It is an apache server running freeBSD 8.2-RELEASE.</p>
  
  <p>Either way, this is part of a class that is included by the code following it:</p>
  
  <pre><code>1600     function download_file($fileDir,$fileName,$instance_name)
  1601     {
  1602         $fileString=$fileDir.'/'.$fileName; // combine the path and file
  1603         // translate file name properly for Internet Explorer.
  1604     if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
  1605     {
  1606           $instance_name = preg_replace('/\./', '%2e', $instance_name,  substr_count($instance_name, '.') - 1);
  1607     }
  1608     // make sure the file exists before sending headers
  1609 
  1610     if(!$fdl=@fopen($fileString,'r'))
  1611         {
  1612            die("Cannot Open File!");
  1613         }
  1614     else
  1615         {
  1616           header("Cache-Control: ");// leave blank to avoid IE errors
  1617           header("Pragma: ");// leave blank to avoid IE errors
  1618           header("Content-type: application/octet-stream");
  1619           header("Content-Disposition: attachment; filename=\"".$instance_name."\"");
  1620           header("Content-length:".(string)(filesize($fileString)));
  1621           sleep(1);
  1622           fpassthru($fdl);
  1623         }
  1624     }
  </code></pre>
  
  <p>And now the code that uses it:</p>
  
  <pre><code>63         function download_att($supp_obj,$a_predefined) 64         {
  65 
  66             $get_vars=$a_predefined['get'];
  67             $att_id=$get_vars['att_id'];
  68             $article_attachment=$supp_obj-&gt;prefix_table("article_attachment");
  69             $sql="select * from $article_attachment where att_id=$att_id";
  70             $a_attach=$supp_obj-&gt;get_a_line($sql);
  71 
  72             $instance_name=$a_attach['attachment_name'];
  73             $att_name_arr=split("\.",$instance_name);
  74
  75             list($n,$ext)=split("\.", $instance_name);
  76
  77
  78             $fileName="kb_".$a_attach['article_id']."_".$att_id.".".$ext;
  79             $fileDir="plugins/knowledgebase/attachments";
  80             $supp_obj-&gt;download_file($fileDir,$fileName,$instance_name);
  81         }
  82 //----------------------------------------------------------------------------------------------------------------------
  83 
  84     if($a_predefined['get']['act']=="artattach")
  85     {
  86         download_att($supp_obj,$a_predefined);
  87         exit;
  88     }
  </code></pre>