您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

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

2024/3/11 23:03:41发布16次查看
怎么开启 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);
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product