PHPでZipファイルに圧縮
対象ディレクトリ内のファイルをZipに圧縮するモジュール
PHP
/**
 * PHPでZipファイルに圧縮
 * 
 * @param string $target_path
 * @param string $zip_name
 * @return boolean
 */
function getZipCompression($target_path, $zip_name = '')
{
    // 存在チェック
    if (! file_exists($target_path)) {
        return FALSE;
    }
    // ZIPファイル名が未定義の場合は圧縮対象ディレクトリの同階層/日付.zip
    if (empty($zip_name)) {
        $zip_name = $target_path . '/' . date('ymd') . '.zip';
    }
    $zip = new ZipArchive();
    try {
        // アーカイブをオープン
        $zip->open($zip_name, ZIPARCHIVE::CREATE);
        // 圧縮対象ディレクトリ
        $targetFiles = scandir($target_path);
        if (! empty($targetFiles)) {
            foreach ($targetFiles as $targetFilesKey => $targetFilesVal) {
                // ファイルのみを抽出
                if (is_file($target_path . '/' . $targetFilesVal)) {
                    // アーカイブに追加
                    $zip->addFile($target_path . '/' . $targetFilesVal, $targetFilesVal);
                }
            }
        }
        // アーカイブをクローズ
        $zip->close();
    } catch (Exception $e) {
        return FALSE;
    }
    return TRUE;
}
使用例
PHP
if (getZipCompression('/home/appli/var/', $zip_name = '/home/appli/www/foo.zip')) {
    echo "成功";
} else {
    echo "失敗";
}