首頁 php教程 php手册 php下载文件源代码(强制任意文件格式下载)

php下载文件源代码(强制任意文件格式下载)

Jun 06, 2016 pm 08:22 PM
php下載文件

有时候我们需要用php下载一些文件,一般就是为了隐藏文件的真实下载地址才需要这样,否则这样会增加服务器负担,不如直接提供软件的地址

一个简单的php文件下载源代码,虽不支持断点续传等,但是可以满足一些常用的需求了。php下载文件其实用一个a标签就能实现,比如 magento-1.8.1.0.zip 。但是遇到一些浏览器能识别的格式,比如.txt,.html,.pdf等,再用abc.txt 想必也知道会发生什么了。

复制代码 代码如下:


/**
 * 文件下载
 *
**/
header("Content-type:text/html;charset=utf-8");
download('web/magento-1.8.1.0.zip', 'magento下载'); 
function download($file, $down_name){
 $suffix = substr($file,strrpos($file,'.')); //获取文件后缀
 $down_name = $down_name.$suffix; //新文件名,就是下载后的名字

 //判断给定的文件存在与否
 if(!file_exists($file)){
  die("您要下载的文件已不存在,可能是被删除");
 }
 $fp = fopen($file,"r");
 $file_size = filesize($file);
 //下载文件需要用到的头
 header("Content-type: application/octet-stream");
 header("Accept-Ranges: bytes");
 header("Accept-Length:".$file_size);
 header("Content-Disposition: attachment; filename=".$down_name);
 $buffer = 1024;
 $file_count = 0;
 //向浏览器返回数据
 while(!feof($fp) && $file_count   $file_con = fread($fp,$buffer);
  $file_count += $buffer;
  echo $file_con;
 }
 fclose($fp);
}
?>

PHP强制性文件下载的源代码

为用户提供强制性的文件下载功能。

复制代码 代码如下:


/********************
*@file - path to file
*/
function force_download($file)
{
if ((isset($file))&&(file_exists($file))) {
header("Content-length: ".filesize($file));
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $file . '"');
readfile("$file");
} else {
echo "No file selected";
}
}

你一定会笑我"下载文件"如此简单都值得说?当然并不是想象那么简单。例如你希望客户要填完一份表格,才可以下载某一文件,,你第一个想法一定是用 "Redirect"的方法,先检查表格是否已经填写完毕和完整,然后就将网址指到该文件,这样客户才能下载,但如果你想做一个关于"网上购物"的电子商务网站,考虑安全问题,你不想用户直接复制网址下载该文件,笔者建议你使用PHP直接读取该实际文件然后下载的方法去做。程序如下:

复制代码 代码如下:


$file_name = "info_check.exe";
$file_dir = "/public/www/download/";
if (!file_exists($file_dir . $file_name)) { //检查文件是否存在
echo "文件找不到";
exit;
} else {
$file = fopen($file_dir . $file_name,"r"); // 打开文件
// 输入文件标签
Header("Content-type: application/octet-stream");
Header("Accept-Ranges: bytes");
Header("Accept-Length: ".filesize($file_dir . $file_name));
Header("Content-Disposition: attachment; filename=" . $file_name);
// 输出文件内容
echo fread($file,filesize($file_dir . $file_name));
fclose($file);
exit;
}

而如果文件路径是"http" 或者 "ftp" 网址的话,则源代码会有少许改变,程序如下:

复制代码 代码如下:


$file_name = "info_check.exe";
$file_dir = "http://www.jb51.net/";
$file = @ fopen($file_dir . $file_name,"r");
if (!$file) {
echo "文件找不到";
} else {
Header("Content-type: application/octet-stream");
Header("Content-Disposition: attachment; filename=" . $file_name);
while (!feof ($file)) {
echo fread($file,50000);
}
fclose ($file);
}

这样就可以用PHP直接输出文件了。

但,一定要注意:Header信息相当于先将文件信息高速浏览器,然后,再把浏览器上的信息下载到附件中。所以,如果在MVC模式的应用程序中,view页一定不要有任何内容,否则,view页的相关内容会随着文件的内容一同被下载,导致下载后的文件不能使用。

下面是我的程序:

 

复制代码 代码如下:


   public function downloadAction()
    {
        if (isset($_GET['mriID']))
    {
    $this->view->mriID=(get_magic_quotes_gpc())?$_GET['mriID']:addslashes($_GET['mriID']);
    }
        if (isset($_GET['dicomID']))
    {
    $this->view->dicomID=(get_magic_quotes_gpc())?$_GET['dicomID']:addslashes($_GET['dicomID']);
    }
       if (isset($_GET['JPGID']))
    {
    $this->view->JPGID=(get_magic_quotes_gpc())?$_GET['JPGID']:addslashes($_GET['JPGID']);
    }
    $dicomfile=new dicomfile();
    $jpgfile=new jpgfile();
    $mri=new mri();
    if($this->view->dicomID)
    {
    $filename=$dicomfile->find($this->view->dicomID)->toArray();
    $filename=$filename[0]['filename'];
    }  
    else if($this->view->JPGID)
    {
      $filename=$jpgfile->find($this->view->JPGID)->toArray();
      $filename=$filename[0]['JPGname'];
    }
    $dir=$mri->find($this->view->mriID)->toArray();
    $dir=$dir[0]['dicom_path'];
    $file=$dir.'/'.$filename;
    if (!file_exists($file))
    {
    echo "the file does not exist!";
    exit();
    }
      $file_size=filesize($file);
           header("Content-type: application/octet-stream");
           header("Accept-Ranges: bytes");
           header("Accept-Length:". $file_size);
           header("Content-Disposition: attachment; filename=".$filename);
           $fp=fopen($file,"r");
      if (!$fp)
    echo "can't open file!";
       $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); 
    }

此时,download.phtml页面一定要是完全空白的。千万不要有任何内容(包括如下的固定信息:



无标题文档)否则,这些信息都将被下载到下载文件中,导致文件不能使用。
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡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

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

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

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

熱門話題

Java教學
1655
14
CakePHP 教程
1413
52
Laravel 教程
1306
25
PHP教程
1252
29
C# 教程
1226
24