簡體   English   中英

如何強制在PHP中下載.xlsx文件而不損壞它?

[英]How force download an .xlsx file in PHP without having it corrupted?

現有的問題都沒有對我有幫助。 我正在嘗試強制下載文件類型為.xlsx的excel文件。 在這樣的下載代碼中,它可以正常工作:

echo "<a href='" . $dateiname . "'>Datei herunterladen</a>";

但是,每當我嘗試進行強制下載時,它都不會起作用。 我已經嘗試過關於stackoverflow的各種問題的各種標題,最后兩個是

header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $dateiname . '"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Pragma: no-cache');
header('Content-Length: ' . filesize($dateiname));
$objWriter->save('php://output');

header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Transfer-Encoding: binary ");
header('Content-Disposition: attachment; filename='.basename($dateiname));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($dateiname));
$objWriter->save('php://output');

我在創建文件的同一文件中都嘗試過,但在單獨的文件中都嘗試過,但是無論哪種方式,我總是會收到錯誤消息:

Excel無法打開文件(文件名),因為文件格式或文件擴展名無效。 驗證文件沒有被篡改,並且文件擴展名與文件格式匹配。

我服務器上的文件本身看起來還不錯。

我發現了如何使用PHPExcel強制下載xls文件。

header("Content-Disposition: attachment; filename={$fileName}.{$fileFormat}");
header("Content-Type: application/vnd.ms-excel;");
header("Cache-Control: max-age=0");

$objWriter->save('php://output');

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM