当前位置:  编程技术>php
本页文章导读:
    ▪php 强制文件下载的实现代码一例      例子如下: <?php $file = 'monkey.gif'; if (file_exists($file)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename='.basename($.........
    ▪php 下载功能的完美实现代码      先看下最常用的实现方法:   代码示例: <? // 检查 FORM 是否全部填写完毕... if ($form_completed) { Header("Location: http://www./download/info_check.exe"); exit; } ?> 或是如下的情况: <a href="http://ww.........
    ▪php 文件下载类(header头信息的应用实例)      php header头信息应用举例,php文件下载类的代码。 代码示例: <? // 使用范例: // $download=new download('php,exe,html',false); // if(!$download->downloadfile($filename)) // { // echo $download->geterrormsg(); // .........

[1]php 强制文件下载的实现代码一例
    来源: 互联网  发布时间: 2013-12-24

例子如下:

<?php
$file = 'monkey.gif';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
?>

例2,

<?php
$file = '/var/www/html/file-to-download.xyz';
header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
header('Content-Length: ' . filesize($file));
header('Content-Disposition: attachment; filename=' . basename($file));
readfile($file);
?>

例3,readfile with accurate limit rate, using pv linux command

<?php
$file = $_SERVER['QUERY_STRING'];
header("Content-length: ".filesize($file));
header("Content-type: ".mime_content_type($file));
readfileLimit($file);

function readfileLimit($file, $limit='10M')
{
    $f = popen("pv -L $limit '$file'",'r') or return false;
    while(!feof($f))
    {
        echo fread($f, 1024);
        flush();
    }
    fclose($f);
}
?>

例4,解决ie下中文文件名乱码问题)

<?php
  $filename = $_GET['filename'];
   $file = './download/'.$filename;
    $rename = ($_GET['rename']=="") ? $filename : $_GET['rename'];           

    header('Content-Type: application/force-download');
    header("Content-Type: application/octet-stream");        
    header('Content-Length: ' . filesize($file)); 
      
    $ua = $_SERVER["HTTP_USER_AGENT"];  //判断浏览器类型  
    if (preg_match("/MSIE/", $ua)) {
        $rename = iconv("gb2312","UTF-8",$rename);    
        $encoded_filename = urlencode($rename);    
        $encoded_filename = str_replace()("+", " ", $encoded_filename);        
        
        header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');    
    } else if (preg_match("/Firefox/", $ua)) {    
        header('Content-Disposition: attachment; filename*="utf8\'\'' . $rename . '"');    
    } else {    
        header('Content-Disposition: attachment; filename="' . $rename . '"');    
    }     
    
    readfile($file);
    exit;
?>

    
[2]php 下载功能的完美实现代码
    来源: 互联网  发布时间: 2013-12-24

先看下最常用的实现方法:
 

代码示例:
<?
// 检查 FORM 是否全部填写完毕...
if ($form_completed) {
Header("Location: http://www./download/info_check.exe");
exit;
}
?>

或是如下的情况:
<a href="http://www.yourwebl.com/users/download.php?id=124524">开始下载文件</a>
这里利用了ID方式接收要下载文件的编号,然后用“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” 网址的话,则源代码会有少许改变,代码如下:
 

代码示例:
<?php
$file_name = "info_check.exe";
$file_dir = "http://www./";
$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直接输出文件了。以上用到了php header函数,更多内容可以参考:php 文件头部(header)信息详解 。

实现php文件安全下载的代码
 

代码示例:

<?php
//文件安全下载
public function downloads($name){
$name_tmp = explode()("_",$name);
$type = $name_tmp[0];
$file_time = explode(".",$name_tmp[3]);
$file_time = $file_time[0];
$file_date = date("Y/md",$file_time);
$file_dir = SITE_PATH."/data/uploads/$type/$file_date/";

if (!file_exists($file_dir.$name)){
header("Content-type: text/html; charset=utf-8");
echo "File not found!";
exit;
} else {
$file = fopen($file_dir.$name,"r");
Header("Content-type: application/octet-stream");
Header("Accept-Ranges: bytes");
Header("Accept-Length: ".filesize($file_dir . $name));
Header("Content-Disposition: attachment; filename=".$name);
echo fread($file, filesize($file_dir.$name));
fclose($file);
}
}
?>


    
[3]php 文件下载类(header头信息的应用实例)
    来源: 互联网  发布时间: 2013-12-24

php header头信息应用举例,php文件下载类的代码。

代码示例:

<?
// 使用范例:
// $download=new download('php,exe,html',false);
// if(!$download->downloadfile($filename))
// {
// echo $download->geterrormsg();
// }
//by http://www.
class download{
var $debug=true;
var $errormsg='';
var $Filter=array();
var $filename='';
var $mineType='text/plain';
var $xlq_filetype=array();

function download($fileFilter='',$isdebug=true)
{
$this->setFilter($fileFilter);
$this->setdebug($isdebug);
$this->setfiletype();
}

function setFilter($fileFilter)
{
if(empty($fileFilter)) return ;
$this->Filter=explode(',',strtolower()($fileFilter));
}
function setdebug($debug)
{
$this->debug=$debug;
}

function setfilename($filename)
{
$this->filename=$filename;
}

function downloadfile($filename)
{
$this->setfilename($filename);
if($this->filecheck())
{
$fn = array_pop( explode()( '/', strtr( $this->filename, '\\', '/' ) ) );
header( "Pragma: public" );
header( "Expires: 0" ); // set expiration time
header( "Cache-Component: must-revalidate, post-check=0, pre-check=0" );
header( "Content-type:".$this->mineType );
header( "Content-Length: " . filesize( $this->filename ) );
header( "Content-Disposition: attachment; filename=\"$fn\"" );
header( 'Content-Transfer-Encoding: binary' );
readfile( $this->filename );
return true;
}else
{
return false;
}
}
function geterrormsg()
{
return $this->errormsg;
}

function filecheck()
{
$filename=$this->filename;
if(file_exists($filename))
{
$filetype=strtolower(array_pop(explode('.',$filename)));
if(in_array($filetype,$this->Filter))
{
$this->errormsg.=$filename.'不允许下载!';
if($this->debug) exit($filename.'不允许下载!') ;
return false;
}else
{
if ( function_exists( "mime_content_type" ) )
{
$this->mineType = mime_content_type( $filename );
}
if(empty($this->mineType))
{
if( isset()($this->xlq_filetype[$filetype]) ) $this->mineType = $this->xlq_filetype[$filetype];
}
if(!empty($this->mineType))
return true;
else
{
$this->errormsg.='获取'.$filename.'文件类型时候发生错误,或者不存在预定文件类型内';
if($this->debug) exit('获取文件类型出错');
return false;
}
}
}else
{
$this->errormsg.=$filename.'不存在!';
if($this->debug) exit($filename.'不存在!') ;
return false;
}
}

function setfiletype()
{
$this->xlq_filetype['chm']='application/octet-stream';
$this->xlq_filetype['ppt']='application/vnd.ms-powerpoint';
$this->xlq_filetype['xls']='application/vnd.ms-excel';
$this->xlq_filetype['doc']='application/msword';
$this->xlq_filetype['exe']='application/octet-stream';
$this->xlq_filetype['rar']='application/octet-stream';
$this->xlq_filetype['js']="javascript/js";
$this->xlq_filetype['css']="text/css";
$this->xlq_filetype['hqx']="application/mac-binhex40";
$this->xlq_filetype['bin']="application/octet-stream";
$this->xlq_filetype['oda']="application/oda";
$this->xlq_filetype['pdf']="application/pdf";
$this->xlq_filetype['ai']="application/postsrcipt";
$this->xlq_filetype['eps']="application/postsrcipt";
$this->xlq_filetype['es']="application/postsrcipt";
$this->xlq_filetype['rtf']="application/rtf";
$this->xlq_filetype['mif']="application/x-mif";
$this->xlq_filetype['csh']="application/x-csh";
$this->xlq_filetype['dvi']="application/x-dvi";
$this->xlq_filetype['hdf']="application/x-hdf";
$this->xlq_filetype['nc']="application/x-netcdf";
$this->xlq_filetype['cdf']="application/x-netcdf";
$this->xlq_filetype['latex']="application/x-latex";
$this->xlq_filetype['ts']="application/x-troll-ts";
$this->xlq_filetype['src']="application/x-wais-source";
$this->xlq_filetype['zip']="application/zip";
$this->xlq_filetype['bcpio']="application/x-bcpio";
$this->xlq_filetype['cpio']="application/x-cpio";
$this->xlq_filetype['gtar']="application/x-gtar";
$this->xlq_filetype['shar']="application/x-shar";
$this->xlq_filetype['sv4cpio']="application/x-sv4cpio";
$this->xlq_filetype['sv4crc']="application/x-sv4crc";
$this->xlq_filetype['tar']="application/x-tar";
$this->xlq_filetype['ustar']="application/x-ustar";
$this->xlq_filetype['man']="application/x-troff-man";
$this->xlq_filetype['sh']="application/x-sh";
$this->xlq_filetype['tcl']="application/x-tcl";
$this->xlq_filetype['tex']="application/x-tex";
$this->xlq_filetype['texi']="application/x-texinfo";
$this->xlq_filetype['texinfo']="application/x-texinfo";
$this->xlq_filetype['t']="application/x-troff";
$this->xlq_filetype['tr']="application/x-troff";
$this->xlq_filetype['roff']="application/x-troff";
$this->xlq_filetype['shar']="application/x-shar";
$this->xlq_filetype['me']="application/x-troll-me";
$this->xlq_filetype['ts']="application/x-troll-ts";
$this->xlq_filetype['gif']="image/gif";
$this->xlq_filetype['jpeg']="image/pjpeg";
$this->xlq_filetype['jpg']="image/pjpeg";
$this->xlq_filetype['jpe']="image/pjpeg";
$this->xlq_filetype['ras']="image/x-cmu-raster";
$this->xlq_filetype['pbm']="image/x-portable-bitmap";
$this->xlq_filetype['ppm']="image/x-portable-pixmap";
$this->xlq_filetype['xbm']="image/x-xbitmap";
$this->xlq_filetype['xwd']="image/x-xwindowdump";
$this->xlq_filetype['ief']="image/ief";
$this->xlq_filetype['tif']="image/tiff";
$this->xlq_filetype['tiff']="image/tiff";
$this->xlq_filetype['pnm']="image/x-portable-anymap";
$this->xlq_filetype['pgm']="image/x-portable-graymap";
$this->xlq_filetype['rgb']="image/x-rgb";
$this->xlq_filetype['xpm']="image/x-xpixmap";
$this->xlq_filetype['txt']="text/plain";
$this->xlq_filetype['c']="text/plain";
$this->xlq_filetype['cc']="text/plain";
$this->xlq_filetype['h']="text/plain";
$this->xlq_filetype['html']="text/html";
$this->xlq_filetype['htm']="text/html";
$this->xlq_filetype['htl']="text/html";
$this->xlq_filetype['rtx']="text/richtext";
$this->xlq_filetype['etx']="text/x-setext";
$this->xlq_filetype['tsv']="text/tab-separated-values";
$this->xlq_filetype['mpeg']="video/mpeg";
$this->xlq_filetype['mpg']="video/mpeg";
$this->xlq_filetype['mpe']="video/mpeg";
$this->xlq_filetype['avi']="video/x-msvideo";
$this->xlq_filetype['qt']="video/quicktime";
$this->xlq_filetype['mov']="video/quicktime";
$this->xlq_filetype['moov']="video/quicktime";
$this->xlq_filetype['movie']="video/x-sgi-movie";
$this->xlq_filetype['au']="audio/basic";
$this->xlq_filetype['snd']="audio/basic";
$this->xlq_filetype['wav']="audio/x-wav";
$this->xlq_filetype['aif']="audio/x-aiff";
$this->xlq_filetype['aiff']="audio/x-aiff";
$this->xlq_filetype['aifc']="audio/x-aiff";
$this->xlq_filetype['swf']="application/x-shockwave-flash";
}
}
?>

您可能感兴趣的文章:
php header()函数的简单例子
php中header函数的用法举例详解
php header 使用详解
php header函数 文件下载时直接提示保存的代码
php header函数实现文本文件下载的方法
php header头信息应用举例
php 文件头部(header)信息详解
php使用header发送各种类型文件下载的例子
PHP header()函数使用详解
PHP中HEADER头消息详解


    
最新技术文章:
▪PHP函数microtime()时间戳的定义与用法
▪PHP单一入口之apache配置内容
▪PHP数组排序方法总结(收藏)
▪php数组排序方法大全(脚本学堂整理奉献)
▪php数组排序的几个函数(附实例)
▪php二维数组排序(实例)
▪php根据键值对二维数组排序的小例子
▪php验证码(附截图)
▪php数组长度的获取方法(三个实例)
▪php获取数组长度的方法举例
▪判断php数组维度(php数组长度)的方法
▪php获取图片的exif信息的示例代码
▪PHP 数组key长度对性能的影响实例分析
▪php函数指定默认值的方法示例
▪php提交表单到当前页面、提交表单后页面重定...
▪php四舍五入的三种实现方法
▪php获得数组长度(元素个数)的方法
▪php日期函数的简单示例代码
▪php数学函数的简单示例代码
▪php字符串函数的简单示例代码
▪php文件下载代码(多浏览器兼容、支持中文文...
▪php实现文件下载、支持中文文件名的示例代码...
▪php文件下载(防止中文文件名乱码)的示例代码
▪解决PHP文件下载时中文文件名乱码的问题
▪php数组去重(一维、二维数组去重)的简单示例
▪php小数点后取两位的三种实现方法
▪php Redis 队列服务的简单示例
▪PHP导出excel时数字变为科学计数的解决方法
▪PHP数组根据值获取Key的简单示例
▪php数组去重的函数代码示例
 


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3