php文件下载代码

piaoling  2011-02-04 16:38:22

<?
       header("content-type:text/html; charset=utf-8");
       $file_name=$_GET['name']; //服务器的真实文件名
       $file_realName=urldecode($_GET['real']); //数据库的文件名urlencode编码过的
$file_dir="upload/";
$file = fopen($file_dir . $file_name,"r"); // 打开文件
// 输入文件标签
header( "Pragma: public" );
header( "Expires: 0" );
Header("Content-type: application/octet-stream");
Header("Accept-Ranges: bytes");
Header("Accept-Length: ".filesize($file_dir . $file_name));
Header("Content-Disposition: attachment; filename=" . iconv("UTF-8","GB2312//TRANSLIT",$file_realName));
// 输出文件内容
echo fread($file,filesize($file_dir . $file_name));
fclose($file);
exit;
?>

 

 

2,

有时候为了隐藏下载地址而使用,通过一个php文件下载来源,如果在限制地址或非允许下载地址则禁止下载,点击一个链接后,php执行提示文件下载,当然这对文件类型判断要求准确,如果错误可能造成文件无法打开情况

代码如下

<?php
     $filepath = 'http://www.dayanmei.com/upload/aoyun_logo.gif';
     $filename = 'aoyun_logo.gif';
     $filetype = $filetype['gif'] ;

     //文档类型对照表
$filetype = array(
     'chm'=>'application/octet-stream',
     'ppt'=>'application/vnd.ms-powerpoint',
     'xls'=>'application/vnd.ms-excel',
     'doc'=>'application/msword',
     'exe'=>'application/octet-stream',
     'rar'=>'application/octet-stream',
     'js'=>"javascript/js",
     'css'=>"text/css",
     'hqx'=>"application/mac-binhex40",
     'bin'=>"application/octet-stream",
     'oda'=>"application/oda",
     'pdf'=>"application/pdf",
     'ai'=>"application/postsrcipt",
     'eps'=>"application/postsrcipt",
     'es'=>"application/postsrcipt",
     'rtf'=>"application/rtf",
     'mif'=>"application/x-mif",
     'csh'=>"application/x-csh",
     'dvi'=>"application/x-dvi",
     'hdf'=>"application/x-hdf",
     'nc'=>"application/x-netcdf",
     'cdf'=>"application/x-netcdf",
     'latex'=>"application/x-latex",
     'ts'=>"application/x-troll-ts",
     'src'=>"application/x-wais-source",
     'zip'=>"application/zip",
     'bcpio'=>"application/x-bcpio",
     'cpio'=>"application/x-cpio",
     'gtar'=>"application/x-gtar",
     'shar'=>"application/x-shar",
     'sv4cpio'=>"application/x-sv4cpio",
     'sv4crc'=>"application/x-sv4crc",
     'tar'=>"application/x-tar",
     'ustar'=>"application/x-ustar",
     'man'=>"application/x-troff-man",
     'sh'=>"application/x-sh",
     'tcl'=>"application/x-tcl",
     'tex'=>"application/x-tex",
     'texi'=>"application/x-texinfo",
     'texinfo'=>"application/x-texinfo",
     't'=>"application/x-troff",
     'tr'=>"application/x-troff",
     'roff'=>"application/x-troff",
     'shar'=>"application/x-shar",
     'me'=>"application/x-troll-me",
     'ts'=>"application/x-troll-ts",
     'gif'=>"image/gif",
     'jpeg'=>"image/pjpeg",
     'jpg'=>"image/pjpeg",
     'jpe'=>"image/pjpeg",
     'ras'=>"image/x-cmu-raster",
     'pbm'=>"image/x-portable-bitmap",
     'ppm'=>"image/x-portable-pixmap",
     'xbm'=>"image/x-xbitmap",
     'xwd'=>"image/x-xwindowdump",
     'ief'=>"image/ief",
     'tif'=>"image/tiff",
     'tiff'=>"image/tiff",
     'pnm'=>"image/x-portable-anymap",
     'pgm'=>"image/x-portable-graymap",
     'rgb'=>"image/x-rgb",
     'xpm'=>"image/x-xpixmap",
     'txt'=>"text/plain",
     'c'=>"text/plain",
     'cc'=>"text/plain",
     'h'=>"text/plain",
     'html'=>"text/html",
     'htm'=>"text/html",
     'htl'=>"text/html",
     'rtx'=>"text/richtext",
     'etx'=>"text/x-setext",
     'tsv'=>"text/tab-separated-values",
     'mpeg'=>"video/mpeg",
     'mpg'=>"video/mpeg",
     'mpe'=>"video/mpeg",
     'avi'=>"video/x-msvideo",
     'qt'=>"video/quicktime",
     'mov'=>"video/quicktime",
     'moov'=>"video/quicktime",
     'movie'=>"video/x-sgi-movie",
     'au'=>"audio/basic",
     'snd'=>"audio/basic",
     'wav'=>"audio/x-wav",
     'aif'=>"audio/x-aiff",
     'aiff'=>"audio/x-aiff",
     'aifc'=>"audio/x-aiff",
     'swf'=>"application/x-shockwave-flash");
   
    $query = file_get_contents($filepath);    
    
     header("Content-type: ".$filetype);    
     header("Content-Disposition:     inline;     filename     =     ".$filename);    
     header('Cache-Control:     must-revalidate,     post-check=0,     pre-check=0');    
     header('Expires:     0');    
     header('Pragma:     public');    
     echo     $query;
?>
 

类别 :  PHP(78)  |  浏览(3585)  |  评论(0)
发表评论(评论将通过邮件发给作者):

Email: