下载文件

function download($title){
	    set_time_limit(0);//取数据时间长,需要加这句
	    
		$result = getResult();//从数据库取数据
		$content = handle($result);//一般要做些处理
		$title = getTitle();//txt的文件名
		$title = iconv('UTF-8', 'GBK', $title);//如果文件名为中文需要
		
		//创建路径
		$file_path = ROOT_PATH . "/attachment/" ;
		if(!file_exists($file_path)){
		    mkdir($file_path,0777,true);
		}
			
		//创建一 文本文件 存储内容(情况不同用其他文件,excel等)
		$txt = $file_path . $title . ".txt";
		$fp = @fopen($txt, "w+");//txt创建
		fwrite($fp, $content);
		fclose($fp);
		
		//添加到 压缩文件
		$zip = $file_path . 'down.zip';
		$zip_arch = new ZipArchive;
		$zip_arch->open($zip, ZipArchive::OVERWRITE);
		if(file_exists($txt)){
			$zip_arch->addFile($txt, $title.'.txt');//zip创建
		}
		$zip_arch->close(); //关闭
		
		//生成zip
		$file = fopen($zip, 'r'); // 打开文件
		Header("Content-type: application/octet-stream");//输入文件标签
		Header("Accept-Ranges: bytes");
		Header("Accept-Length: ".filesize($zip));
		Header("Content-Disposition: attachment; filename=" . "down.zip");
		echo fread($file,filesize($zip));//下载
		fclose($file);
	}


/**
 * 文件下载
 * @param string $path	文件路径
 * @param string $fileName 文件名
 * @param string $reFileName 另存为文件名 
 */
function getDownLoadFile($path,$fileName,$reFileName = ''){
	header("Content-type:text/html;charset=utf-8");
	if($reFileName == ''){
		$reFileName = $fileName;
	}
	//用以解决中文不能显示出来的问题
	$file_name = iconv("utf-8","gb2312",$reFileName);
	$file_path = $path . $fileName;
	//首先要判断给定的文件存在与否
	if(!file_exists($file_path)){
		return "没有该文件文件";
		exit;
	}
	$fp = fopen($file_path,"r");
	$file_size = filesize($file_path);
	//下载文件需要用到的头
	Header("Content-type: application/octet-stream");
	Header("Accept-Ranges: bytes");
	Header("Accept-Length:".$file_size);
	Header("Content-Disposition: attachment; filename=".$file_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);
}


摘自 项目中

main/app/articleDown.app.php
comm.function.php getDownLoadFile


未完成

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值