我遇到一個問題,其中唯一能夠從服務器正確下載的文件類型是「.pdf」。其他文件類型的其他文件存在於相同的位置,上傳完全正確,具有正確的權限,並且當我將它們掃描到本地計算機時加載得很好。我不能顯示所涉及的代碼的全部,因爲這是付費軟件,但老實說,似乎問題不應該在於代碼本身...PHP文件下載給任何東西垃圾但是.pdf文件
我試過重寫第一個函數下面是類似於這樣的內容:http://www.finalwebsites.com/forums/topic/php-file-download,然後簡單地給它提供正確的值,使其從服務器中選擇特定的文件。這些文件下載相同的方式。它們會顯示爲正確的文件類型,並具有正確的名稱,甚至正確的大小,但它們不會以任何形式打開,除非是垃圾值(除非是PDF)。它是運行freeBSD 8.2-RELEASE的apache服務器。
無論哪種方式,這是繼它是由代碼中包含了一個類的一部分:使用它
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([email protected]($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 }
而現在的代碼:
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->prefix_table("article_attachment");
69 $sql="select * from $article_attachment where att_id=$att_id";
70 $a_attach=$supp_obj->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->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 }
並且這些文件在服務器上的格式是否正確? – 2011-06-03 20:26:37
禁用您的網絡服務器(和緩存)的輸出壓縮,以及PHP端的輸出壓縮。然後檢查問題是否仍然存在。 – hakre 2011-06-03 20:27:13
即使禁用了輸出壓縮,也會出現此問題。 – 2011-06-03 20:43:34