下载文件

来源:互联网 发布:淘宝如何搜索苍蝇水 编辑:程序博客网 时间:2024/06/05 19:51
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
原创粉丝点击