0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【過去ログ 2022】PHP-ZIP

0
Posted at
$file_name = 'cqi-' . $date->format('Ymd') . '-' . 'id' . strval($iid) . $extension;

$data_path = Storage::path("attachments/product/{$aid}/{$item_name}");

private static function imgZipDownload($file_infos){
    $date = Carbon::now();

    //■zipファイル
    // 出来上がるzipのファイル名

    $zip_file_name = "cqi-{$date->format('Ymd')}-{$date->format('His')}.zip";

    $tmp = "tmpzip";
    if (!file_exists($tmp)) {
        if (!mkdir($tmp, 0777, true)) {
            abort(400,"{$tmp}の作成に失敗しました");
        }
    }

    $zip = new ZipArchive();
    $zip->open("tmpzip/{$zip_file_name}", ZipArchive::CREATE);

    foreach($file_infos as $f){
        // zipにしたいファイルを追加
        // $zip->addFile('img/sumi.png', 'sumi.png');
        $zip->addFile($f->path,$f->name);
    }
    // zipファイル作成
    $zip->close();


    //■自動ダウンロード
    header('Content-Type: application/zip; name="' . $zip_file_name . '"');
    header('Content-Disposition: attachment; filename="' . $zip_file_name . '"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize("tmpzip/{$zip_file_name}"));
    // header('Content-Length: ' . filesize($zipDir . $zipFileName));
    header('Connection: close');
    while (ob_get_level()) {
        ob_end_clean();
    }
    readfile("tmpzip/{$zip_file_name}");
    unlink("tmpzip/{$zip_file_name}");

    exit;
}
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?