這篇文章主要介紹了關於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檔打壓縮包的詳細內容。更多資訊請關注PHP中文網其他相關文章!