PHP文件下载类源码

2009-05-28 15:49:37 作者:admin 来源: 浏览次数:0 网友评论 0

<?
class dl{
/*
提示语言种类
language configure
*/
var $language="CN";
/*
默认允许下载的文件类型
type of file which allow to down load
is array
*/
var $allow=array(".jpg",".txt",".gif",".png");
/*
文件的默认路径,默认是当前目录
default path of down load.default is this direction.
*/
var $serverPath="./";

/*
定义出错信息
define error message
*/
var $message_CN=array(
"0"=>"未找到此文件.",
"1"=>"不允许下载此类文件."
);
var $message_EN=array(
"0"=>"this file does not exit.",
"1"=>"this file is deny."
);
function __construct($serverPath){
$this->serverPath=$serverPath;
if($this->language=="CN"){
$this->message=$this->message_CN;
}else{
$this->message=$this->message_EN;
}
}
/*
检查文件是否存在并检测是否允许下载此类型的文件
*/
function get_ext($file){
$file_ext=substr($file,-4);

if(!file_exists($this->serverPath."/".$file)){
$this->error("0");
}
if(!in_array($file_ext,$this->allow)){
$this->error("1");
}
}
function down($filename, $mimeType = 'application/octet-stream'){
$this->get_ext($filename);
$fullname=$this->serverPath."/".$filename;
header("Content-Type: {$mimeType}");
$filename = '"' . htmlspecialchars($filename) . '"';
$filesize = filesize($fullname);
header("Content-Disposition: attachment; filename={$filename}; charset=gb2312");
header("Content-Length: {$filesize}");
readfile($fullname);
exit;
}

function error($i){
die($this->message[$i]);
}
/*
end of class
*/
}
?>


函数描述及例子



<?
$d=new dl("../images");
$d->down("coffee_01.gif");
?>


[错误报告] [推荐] [收藏] [打印] [关闭] [返回顶部]

  • 验证码:

最新图片文章

最新文章