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

php中文网
发布: 2016-06-23 13:28:34
原创
1181人浏览过

怎么开启 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);
登录后复制

多个文件打包下载实例:

有道小P
有道小P

有道小P,新一代AI全科学习助手,在学习中遇到任何问题都可以问我。

有道小P 64
查看详情 有道小P
$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速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号