LoginSignup
2
5

More than 5 years have passed since last update.

php フォルダ丸ごと zip 圧縮

Posted at

phpでフォルダごと圧縮した場合がある。
そんな時は、linux コマンドを使うべき。


//$dir 取得したいフォルダパス
//$zipFileSavePath 一時、zipを保存しておくフォルダパス
function getZip($dir,$zipFileSavePath){

 // zipファイル名
    $fileName = "zipFile".time();
// 圧縮対象フォルダ
    $compressDir = $dir;

// コマンド
// cd:ディレクトリの移動
// zip:zipファイルの作成
    $command =  "cd ". $compressDir .";".
        "zip -r ". $zipFileSavePath . $fileName .".zip .";

// Linuxコマンドの実行
    exec($command);

// 圧縮したファイルをダウンロードさせる。
    header('Pragma: public');
    header("Content-Type: application/octet-stream");
    header("Content-Disposition: attachment; filename=".$fileName.".zip");
    readfile($zipFileSavePath.$fileName.".zip");

//    消す
    unlink($zipFileSavePath.$fileName.".zip");
}


cakephp3の場合の使い方


        getZip(WWW_ROOT.'files/templates/hogedir,WWW_ROOT.'files/');

こんな感じかな。

2
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
5