程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php實現當前頁面點擊下載文件的簡單方法,

php實現當前頁面點擊下載文件的簡單方法,

編輯:關於PHP編程

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="" ></iframe>
$(function(){
      $('.downLoad').click(function(){
        var path = $(this).attr('path');
        $('#iframe').attr('src',"php文件路徑?path="+path);
      })
    })

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

以上就是小編為大家帶來的php實現當前頁面點擊下載文件的簡單方法的全部內容了,希望對大家有所幫助,多多支持幫客之家~

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved