Re: [請益] Header 下載的一個問題

看板PHP作者 (小惡魔)時間16年前 (2009/12/18 14:50), 編輯推噓1(102)
留言3則, 2人參與, 最新討論串2/4 (看更多)
※ 引述《JYHuang (夏天到了,冷不起來了說)》之銘言: : 之前寫了一個檔案下載的程式 getfile.php : 利用Header 下載或開啟檔案。 : 不過最近遇到一個情形是 : 如果檔案是還沒有被支援的格式(像是沒灌Acrobat Reader遇到.pdf) : $filename = "manul.pdf" : header("Content-Type:application/pdf"); : header("Content-Disposition: inline ; filename={$filename}\n"); : header("Content-Transfer-Encoding: binary"); : header("Content-Length: ".filesize($filename)); : @readfile($filename); : 在IE下會變成要如何處理"getfile.php"的下載對話框 : Firefox是會出現要如何處理"manul.pdf"的對話框 : 這裡是要讓browser直接開啟檔案 : 請問http header的格式 能不能在browser遇到不認識的檔案時 : 變成下載指定檔案。 : ..還是這又是IE下無解的問題 試試看底下: // Generate the server headers if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) { header('Content-Type: "'.$mime.'"'); header('Content-Disposition: attachment; filename="'.iconv('utf-8', 'big5', $filename).'"'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header("Content-Transfer-Encoding: binary"); header('Pragma: public'); header("Content-Length: ".strlen($data)); } else { header('Content-Type: "'.$mime.'"'); header('Content-Disposition: attachment; filename="'.$filename.'"'); header("Content-Transfer-Encoding: binary"); header('Expires: 0'); header('Pragma: no-cache'); header("Content-Length: ".strlen($data)); } 上面是 CodeIgniter helper 裡面的函數,擷取出來修改的...可以參考底下連結 http://blog.wu-boy.com/2009/10/26/1757/ -- PHP MVC CodeIgniter 繁體中文手冊: http://tinyurl.com/mduyv8 PHP MVC CodeIgniter 繁體中文討論區:http://tinyurl.com/yayzoz8 PHP MVC CodeIgniter 中文官方網站: http://tinyurl.com/yatds8n Appleboy Blog 電腦技術: http://blog.Wu-Boy.com -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.123.107.202

12/18 16:20, , 1F
感謝..不過我要針對的是Browser"直接開啟"而不是附檔下載
12/18 16:20, 1F

12/18 19:08, , 2F
直接開啟就不要用 php 去開檔案,直接給連結就好
12/18 19:08, 2F

12/18 19:09, , 3F
如果不給連結 那給 Content-Type: 就好
12/18 19:09, 3F
文章代碼(AID): #1BAoOoxZ (PHP)
文章代碼(AID): #1BAoOoxZ (PHP)