国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 武夷山市| 赫章县| 类乌齐县| 新平| 如皋市| 额济纳旗| 奇台县| 资源县| 库尔勒市| 合阳县| 平乡县| 乌拉特中旗| 名山县| 芒康县| 石阡县| 新巴尔虎右旗| 洪江市| 六安市| 兴城市| 远安县| 亚东县| 华池县| 黑河市| 英山县| 新乡县| 金川县| 开平市| 名山县| 利津县| 柳江县| 萝北县| 高青县| 九龙城区| 绥宁县| 洛隆县| 岳普湖县| 遂昌县| 武夷山市| 利辛县| 康平县| 东乡族自治县|