国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php下實現(xiàn)文件下載實現(xiàn)代碼

2024-09-04 11:44:44
字體:
供稿:網(wǎng)友

文章介紹了利用php來實現(xiàn)讀取文件并且下載的代碼,php要下載文件必須用到header函數(shù),大家可參考一下,代碼如下:

  1. <?php 
  2. $file = 'monkey.gif'
  3. if (file_exists($file)) { 
  4. header('Content-Description: File Transfer'); 
  5. header('Content-Type: application/octet-stream'); 
  6. header('Content-Disposition: attachment; filename='.basename($file)); 
  7. header('Content-Transfer-Encoding: binary'); 
  8. header('Expires: 0'); 
  9. header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
  10. header('Pragma: public'); 
  11. header('Content-Length: ' . filesize($file)); 
  12. ob_clean(); 
  13. flush(); 
  14. readfile($file); 
  15. exit
  16. ?> 

以上代碼是下載代碼,接下來貼一段在線預覽pdf文件的代碼:

  1. <?php 
  2. public function fddAction() 
  3. // get attachment location 
  4. $attachment_location = $_SERVER["DOCUMENT_ROOT"] . "/pdf/fdd/sample.pdf"
  5.  
  6. if (file_exists($attachment_location)) { 
  7. // attachment exists 
  8.  
  9. // send open pdf dialog to user 
  10. header('Cache-Control: public'); // needed for i.e. 
  11. header('Content-Type: application/pdf'); 
  12. header('Content-Disposition: inline; filename="sample.pdf"'); 
  13. readfile($attachment_location); 
  14. die(); // stop execution of further script because we are only outputting the pdf 
  15.  
  16. else { 
  17. die('Error: File not found.'); 
  18. ?>

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 九江县| 天津市| 天水市| 合川市| 鹿泉市| 武乡县| 老河口市| 平利县| 东宁县| 桦南县| 黄龙县| 措勤县| 临漳县| 谢通门县| 河池市| 台前县| 伊宁市| 巫山县| 永善县| 布尔津县| 四子王旗| 安阳县| 新建县| 洛阳市| 怀柔区| 新闻| 丹巴县| 新乡市| 彭山县| 丰顺县| 辽阳市| 剑阁县| 吐鲁番市| 长白| 平谷区| 弥勒县| 清新县| 十堰市| 扎赉特旗| 吉林省| 盐亭县|