close
轉自 https://www.796t.com/content/1550581594.html
--
PHP檔案打包成zip,包含資料夾
使用ZipArchive class
- 開始先new一個$zip,$zip = new ZipArchive;
- 然後把(此檔案如果是資料夾這裡迴圈)資料夾路徑,和$zip,還有就是初始的檔案夾了 3. 迴圈資料夾檔案 4. 去掉資料夾中的.和.. 5. 再判斷此路徑還有資料夾,如果是資料夾,再次執行本函式 6. 如果不是加入檔案
- 完成後closedir();
- 最後$zip->close();
不多說了,上圖
下面是程式碼可以複製
// Add file to ZIP
function add_file_to_zip($path,$zip,$base_path)
{
if(is_dir($path))
{
$handler = opendir($path);
while(($file = readdir($handler)) !== false )
{
if($file != "." && $file != "..")
{
if(is_dir($path."/".$file))
{
$this->add_file_to_zip($path."/".$file, $zip, $base_path);
}
else
{
$dir_path = explode($base_path, $path);
//var_dump($dir_path);
$zip->addFile($path."/".$file,$dir_path[1].'/'.$file);
}
}
}
closedir($path);
}
else
{
echo "資料夾不存在";
}
}
--
全站熱搜