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

首頁 > 語言 > PHP > 正文

php強制文件下載而非在瀏覽器打開的自定義函數分享

2024-09-04 11:43:23
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了php強制文件下載而非在瀏覽器打開的自定義函數分享,需要的朋友可以參考下。

有時我們希望如圖片、文本文檔、網頁、mp3、pdf等內容,當點擊對應鏈接時直接下載,而不是在網頁上顯示,那么就需要強制設置header頭信息。以下為一段不會產生亂碼的php函數實現代碼,其他程序語言也可參考之編寫實現,代碼如下:

  1. /** 
  2.  * Downloader 
  3.  * 
  4.  * @param $archivo 
  5.  *  path al archivo 
  6.  * @param $downloadfilename 
  7.  *  (null|string) el nombre que queres usar para el archivo que se va a descargar. 
  8.  *  (si no lo especificas usa el nombre actual del archivo) 
  9.  * 
  10.  * @return file stream 
  11.  */ 
  12. function download_file($archivo$downloadfilename = null) { 
  13.     if (file_exists($archivo)) { 
  14.         $downloadfilename = $downloadfilename !== null ? $downloadfilename : basename($archivo); 
  15.         header('Content-Description: File Transfer'); 
  16.         header('Content-Type: application/octet-stream'); 
  17.         header('Content-Disposition: attachment; filename=' . $downloadfilename); 
  18.         header('Content-Transfer-Encoding: binary'); 
  19.         header('Expires: 0'); 
  20.         header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
  21.         header('Pragma: public'); 
  22.         header('Content-Length: ' . filesize($archivo)); 
  23.  
  24.         ob_clean(); 
  25.         flush(); 
  26.         readfile($archivo); 
  27.         exit
  28.     } 
  29.  

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 诸城市| 三台县| 汾西县| 汤原县| 略阳县| 达拉特旗| 墨竹工卡县| 罗城| 三江| 祁东县| 穆棱市| 孝感市| 原阳县| 吐鲁番市| 黄平县| 高唐县| 苏尼特左旗| 河西区| 金溪县| 普宁市| 沙洋县| 漠河县| 张北县| 甘德县| 麟游县| 太湖县| 白山市| 航空| 尼玛县| 屯昌县| 淮北市| 江山市| 大兴区| 嘉定区| 平凉市| 石门县| 彭泽县| 南昌县| 红安县| 清徐县| 西吉县|