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="" style="display: none;"></iframe>
$(function(){ $('.downLoad').click(function(){ var path = $(this).attr('path'); $('#iframe').attr('src',"php文件路徑?path="+path); }) })總結
以上是生活随笔為你收集整理的php实现当前页面点击下载文件的实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 中华职业学院是几本(中华职业学院)
- 下一篇: 上下结构开火念什么(开火念什么)