搜尋
首頁php教程PHP源码php 实现文件下载

<script>ec(2);</script>

//author:heaven
//文件下载
function download($file_dir,$file_name)
//参数说明:
//file_dir:文件所在目录
//file_name:文件名
{
     $file_dir = chop($file_dir);//去掉路径中多余的空格
     //得出要下载的文件的路径
     if($file_dir != '')
     {
         $file_path = $file_dir;
         if(substr($file_dir,strlen($file_dir)-1,strlen($file_dir)) != '/')
             $file_path .= '/';
         $file_path .= $file_name;
     }           
     else
         $file_path = $file_name;   

   
     //判断要下载的文件是否存在
     if(!file_exists($file_path))
     {
         echo '对不起,你要下载的文件不存在。';
         return false;
     }

     $file_size = filesize($file_path);

     header("Content-type: application/octet-stream");
     header("Accept-Ranges: bytes");
     header("Accept-Length: $file_size");
     header("Content-Disposition: attachment; filename=".$file_name);
 
  //echo file_get_contents($file_path);
   
     $fp = fopen($file_path,"r");
     $buffer_size = 1024;
     $cur_pos = 0;
   
     while(!feof($fp)&&$file_size-$cur_pos>$buffer_size)
     {
         $buffer = fread($fp,$buffer_size);
         echo $buffer;
         $cur_pos += $buffer_size;
     }
   
     $buffer =@fread($fp,$file_size-$cur_pos);
     echo $buffer;
  
     fclose($fp);
     return true;
 

}

 

 

页面处理部分

$dir = "../main/trade_log";  
// Open a known directory, and proceed to read its contents  
if (is_dir($dir))
 {  
     $filename=array();
    if ($dh = opendir($dir))
    {  
     while (($file = readdir($dh)) !== false)
   {  
      if ($file!="." && $file!=".." && $file!=".svn")
    {

           $filename[]=$file;
    }  
    }  
     closedir($dh);  
    }  
}  

if(isset($_GET['logname']))
{

   download("../main/trade_log",$_GET['logname']);
   exit;

}
$smarty->assign("trade_log_list",$filename);

 

注意这块

   download("../main/trade_log",$_GET['logname']);
   exit;

一定要加exit

否则下载的文件display的部分也要输出

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版