时间:2021-07-01 10:21:17 帮助过:3人阅读
这篇文章主要介绍了关于php文件打压缩包,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
为了方便以后重用,这里我写了三个函数,必须一起放到一个类下边,读者也可根据情况分开,但请注意更改方法调用的类名(这里都用的self)。
Question:这里我想只把当前路径下所有文件(包括文件夹)压缩到压缩包里,不想压缩多余的文件夹,不知道该怎么做,有望知情者告知,谢谢!
/** * add path to zip * Here, I want to add all the files under the current path, but I don't know how to add it, I want to get help, thank you! * @param $path * @param $zip */ public static function addDirectoryToZip($path, $zip) { self::createPath([$path, $zip]); $zipObj = new \ZipArchive(); if ($zipObj->open($zip, \ZipArchive::CREATE) === TRUE) { $handler = opendir($path); while (($filename = readdir($handler))) { if ($filename != "." && $filename != "..") { $tem_path = $path . DIRECTORY_SEPARATOR . $filename; if (is_dir($tem_path)) { self::addDirectoryToZip($tem_path, $zip); } else { $zipObj->addFromString(iconv('gbk', 'utf-8', $tem_path), file_get_contents($tem_path)); } } } @closedir($path); $zipObj->close(); } } /** * Create a or more path * @param $path */ public static function createPath($path) { if (is_array($path)) { foreach ($path AS $v) { if (!is_array($v)) { self::createOnePath($v); } else { die('Must be a one-dimensional array!'); } } } else { self::createOnePath($path); } } /** * Create a path * @param $path */ public static function createOnePath($path) { $path = strpos(basename($path), '.') ? dirname($path) : $path; if (!is_dir($path)) {//nonexistion path mkdir($path, 0777, true) ? '' : die('Creation path failed!'); } }
相关推荐:
几行代码轻松实现PHP文件打包下载zip
PHP文件加锁确保多线程写入安全
以上就是php文件打压缩包的详细内容,更多请关注Gxl网其它相关文章!