码迷,mamicode.com
首页 > Web开发 > 详细

PHP学习笔记-非常有用的 PHP 代码片段(1)

时间:2015-08-16 00:56:05      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

怎么开启 ZipArchive 扩展 请可自行百度。

直接上代码

//单个文件插入Zip包
function addFileToZip($path, $zip) {
	$handler = opendir($path); //打开当前文件夹由$path指定。
	/*
	循环的读取文件夹下的所有文件和文件夹
	其中$filename = readdir($handler)是每次循环的时候将读取的文件名赋值给$filename,
	为了不陷于死循环,所以还要让$filename !== false。
	一定要用!==,因为如果某个文件名如果叫‘0‘,或者某些被系统认为是代表false,用!=就会停止循环
	*/
	while (($filename = readdir($handler)) !== false) {
		if ($filename != "." && $filename != "..") {//文件夹文件名字为‘.‘和‘..’,不要对他们进行操作
			if (is_dir($path . "/" . $filename)) {// 如果读取的某个对象是文件夹,则递归
				addFileToZip($path . "/" . $filename, $zip);
			} else { //将文件加入zip对象
				$zip->addFile($path . "/" . $filename);
			}
		}
	}
	@closedir($path);
}


/* 
 *   @creates a compressed zip file  将多个文件压缩成一个zip文件的函数 
 *   @$files 数组类型  实例array("1.jpg","2.jpg");   
 *   @destination  目标文件的路径  如"c:/androidyue.zip" 
 *   @$overwrite 是否为覆盖与目标文件相同的文件 
 */  
function create_zip($files = array(),$destination = ‘‘,$overwrite = false) {  
    //if the zip file already exists and overwrite is false, return false  
    //如果zip文件已经存在并且设置为不重写返回false  
    if(file_exists($destination) && !$overwrite) { return false; }  
    //vars  
    $valid_files = array();  
    //if files were passed in...  
    //获取到真实有效的文件名  
    if(is_array($files)) {  
        //cycle through each file  
        foreach($files as $file) {  
        //make sure the file exists  
            if(file_exists($file)) {  
            $valid_files[] = $file;  
            }  
        }  
    }  
    //if we have good files...  
    //如果存在真实有效的文件  
    if(count($valid_files)) {  
        //create the archive  
        $zip = new ZipArchive();  
        //打开文件       如果文件已经存在则覆盖,如果没有则创建  
        if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {  
            return false;  
        }  
        //add the files  
        //向压缩文件中添加文件 
        if ($zip->open($destination, ZipArchive::OVERWRITE) === TRUE) {
           foreach($valid_files as $file) {  
              addFileToZip($file, $zip); //调用方法,对要打包的根目录进行操作,并将ZipArchive的对象传递给方法
           } 
	} 
        //debug  
        //echo ‘The zip archive contains ‘,$zip->numFiles,‘ files with a status of ‘,$zip->status;  
        //close the zip -- done!  
        //关闭文件  
        $zip->close();  
        //check to make sure the file exists  
        //检测文件是否存在  
        return file_exists($destination);  
    }else{  
        //如果没有真实有效的文件返回false  
        return false;  
    }  
}

单个文件下载实例:

$filename = ‘../download/zipfilename.zip‘; //压缩文件的路径

$zip = new ZipArchive();
if ($zip->open($filename, ZipArchive::OVERWRITE) === TRUE) {
    addFileToZip("../folder", $zip); //调用方法,对要打包的根目录进行操作,并将ZipArchive的对象传递给方法
    $zip->close(); //关闭处理的zip文件
}

header("Cache-Control: public");  
header("Content-Description: File Transfer");  
header(‘Content-disposition: attachment; filename=‘.basename($filename)); //文件名  
header("Content-Type: application/zip"); //zip格式的  
header("Content-Transfer-Encoding: binary");    //告诉浏览 器,这是二进制文件  
header(‘Content-Length: ‘. filesize($filename));    //告诉浏览 器,文件大小  
@readfile($filename);

多个文件打包下载实例:

$filsarr = array("../folder1", "../folder2", "../folder5", "../folder11"); //可挑选各个目录下的内容
$filename = ‘../download/zipfilename.zip‘; //压缩文件的路径

create_zip($filsarr, $filename);

header("Cache-Control: public");  
header("Content-Description: File Transfer");  
header(‘Content-disposition: attachment; filename=‘.basename($filename)); //文件名  
header("Content-Type: application/zip"); //zip格式的  
header("Content-Transfer-Encoding: binary");    //告诉浏览 器,这是二进制文件  
header(‘Content-Length: ‘. filesize($filename));    //告诉浏览 器,文件大小  
@readfile($filename);


PHP学习笔记-非常有用的 PHP 代码片段(1)

标签:

原文地址:http://my.oschina.net/xiax/blog/493032

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!