用户文件下载的类。可以自定义下载文件类型,默认为:".jpg", ".txt", ".gif", ".png", ".rar"。属于InitPHP框架扩展类,需要通过$this->getLibrary()方法获取
$down = $this->getLibrary('download');
class indexController extends Controller {
public $initphp_list = array('test');
public function run() {
$down = $this->getLibrary('download');
$down->down('www.rar');
$this->view->set_tpl('test'); //设置一个test.htm的模板页面
$this->view->display(); //模板显示
}
public function test() {
echo 'index.php?c=index&a=test 才会执行';
$code = $this->getLibrary('code');
$code->getcode();
}
/**
* @return testService
*/
private function getTestService() {
return InitPHP::getService('test','test');
}
}
if (!defined('IS_INITPHP')) exit('Access Denied!');
/*********************************************************************************
* InitPHP 2.1 国产PHP开发框架 扩展类库-下载类
*-------------------------------------------------------------------------------
* 版权所有: CopyRight By initphp.com
* 您可以自由使用该源码,但是在使用过程中,请保留作者信息。尊重他人劳动成果就是尊重自己
*-------------------------------------------------------------------------------
* $Author:zhuli
* $Dtime:2011-10-09
***********************************************************************************/
class downloadInit {
private $allow = array(".jpg", ".txt", ".gif", ".png", ".rar"); //允许下载的文件类型
/**
* 文件下载
* @param string $file_name 文件名
* @param string $server_path 文件目录
* @param string $mime_type 传输类型
* @return
*/
public function down($file_name, $server_path = './', $mime_type = 'application/octet-stream') {
$full_file_name = $server_path . '/' . $file_name;
$this->check_file_ext($file_name);
$this->check_file_exists($full_file_name);
header("Content-Type: {$mime_type}");
$file_name = '"' . htmlspecialchars($file_name) . '"';
$file_size = filesize($full_file_name);
header("Content-Disposition: attachment; filename={$file_name}; charset=utf-8");
header("Content-Length: {$file_size}");
readfile($full_file_name);
exit;
}
/**
* 检测文件类型
* @param string $file_name 文件名
* @return
*/
private function check_file_ext($file) {
$file_ext = strtolower(substr($file, -4));
if (!in_array($file_ext, $this->allow)) exit('this file is deny!');
return true;
}
/**
* 检测文件是否存在
* @param string $full_file_name 带目录的文件名
* @return
*/
private function check_file_exists($full_file_name) {
if (!file_exists($full_file_name)) exit('this file does not exit!');
return true;
}
}