ゆう's Blog
作成したファイルをダウンロード後に削除する

// ファイル名をサニタイズしてセキュリティを確保 $file = basename($file); $filePath = './uploads/' . $file; if (file_exists($filePath)) { // ダウンロードを開始するためのヘッダーを設定 header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . $file . '"'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Content-Length: ' . filesize($filePath)); // 出力バッファをクリア ob_clean(); flush(); // ファイルを読み込んで出力 readfile($filePath); // ダウンロード後にファイルを削除 unlink($filePath); exit; } else { echo 'ファイルが存在しません。'; }