上傳 (Upload) 與下載是**兩個獨立動作**。上傳儲存檔案至伺服器磁碟 (或雲端),下載則由控制器讀取檔案串流輸出至瀏覽器。
attachment)。public function download($id) {
$file = Db::name('attachment')->find($id);
if (!$file) $this->error('檔案不存在');
$path = $file['save_path'] . $file['save_name'];
if (!is_file($path)) $this->error('實體檔案遺失');
// 設定標頭強制下載
header('Content-Type: ' . $file['mime']);
header('Content-Disposition: attachment; filename="' . $file['original_name'] . '"');
header('Content-Length: ' . filesize($path));
readfile($path);
exit;
}
<a href="/download/123">下載附件</a>。X-Sendfile (Nginx/Apache 模組) 或分塊輸出,避免記憶體耗盡。