服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - PHP教程 - php下载文件源代码(强制任意文件格式下载)

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

2020-06-28 12:55PHP技术网 PHP教程

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

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

 

复制代码 代码如下:


<?php
/**
 * 文件下载
 *
**/
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_size){
  $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.zzvips.com/";
$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页面一定要是完全空白的。千万不要有任何内容(包括如下的固定信息:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>)否则,这些信息都将被下载到下载文件中,导致文件不能使用。

延伸 · 阅读

精彩推荐