php實作目前頁面點選下載檔案的方法

墨辰丷
發布: 2023-03-28 21:48:01
原創
1971 人瀏覽過

本篇文章主要介紹php實作目前頁點選下載檔的方法,有興趣的朋友參考下,希望對大家有幫助。

php控制器中程式碼

public function downFile($path = ''){
    if(!$path) header("Location: /");
    download($path);
  }
登入後複製

download檔下載函數程式碼

function download($file_url,$new_name=''){ 
    if(!isset($file_url)||trim($file_url)==''){ 
      echo '500'; 
    } 
    if(!file_exists($file_url)){ //检查文件是否存在 
      echo '404'; 
    } 
    $file_name=basename($file_url); 
    $file_type=explode('.',$file_url); 
    $file_type=$file_type[count($file_type)-1]; 
    $file_name=trim($new_name=='')?$file_name:urlencode($new_name); 
    $file_type=fopen($file_url,'r'); //打开文件 
    //输入文件标签 
    header("Content-type: application/octet-stream"); 
    header("Accept-Ranges: bytes"); 
    header("Accept-Length: ".filesize($file_url)); 
    header("Content-Disposition: attachment; filename=".$file_name); 
    //输出文件内容 
    echo fread($file_type,filesize($file_url)); 
    fclose($file_type);
}
登入後複製

html程式碼

#
<iframe id="iframe" src="" style="display: none;"></iframe>
$(function(){
      $(&#39;.downLoad&#39;).click(function(){
        var path = $(this).attr(&#39;path&#39;);
        $(&#39;#iframe&#39;).attr(&#39;src&#39;,"php文件路径?path="+path);
      })
    })
登入後複製

##把上面的程式碼,分別放到你的程式中執行就可以了。

總結:以上就是這篇文章的全部內容,希望能對大家的學習有所幫助。

相關推薦:

PHP仿微信發紅色包領紅包效果

php將服務端的檔案讀出來顯示在web頁面實例

利用php做伺服器和web前端的介面進行互動

#

以上是php實作目前頁面點選下載檔案的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!