作成したファイルをダウンロード後に削除する
// ファイル名をサニタイズしてセキュリティを確保 $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)); // 出力バッファをクリア if (ob_get_level() > 0) { ob_clean(); } flush(); // ファイルを読み込んで出力 readfile($filePath); // ダウンロード後にファイルを削除 unlink($filePath); exit; } else { echo 'ファイルが存在しません。'; }