5
Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.
Tôi cần tải xuống tệp xlsx
từ trang web của mình [nhưng không phải từ URL tệp mở trực tiếp như thế này: //site.com/file.xlsx
]
Vì vậy, đây là mã PHP
$file = "somefile.xlsx";
header['Content-Description: File Transfer'];
header['Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'];
header['Content-Disposition: attachment; filename='.basename[$file]];
header['Content-Transfer-Encoding: binary'];
header['Expires: 0'];
header['Cache-Control: must-revalidate'];
header['Pragma: public'];
header['Content-Length: ' . filesize[$file]];
ob_clean[];
flush[];
readfile[$file];
Tệp được tải xuống, tiện ích mở rộng của anh ấy là .xlsx
, nhưng khi thử Mở tệp này trong MS Excel, Tệp không được mở và tôi đã gặp lỗi: excel cannot open the file.xlsx because the file format or file extension is not valid
Làm ơn nói, tại sao điều này xảy ra? Tôi sai ở đâu?
Hỏi ngày 27 tháng 4 năm 2013 lúc 9:23Apr 27, 2013 at 9:23
Oto Shavadzeoto ShavadzeOto Shavadze
38.6K54 Huy hiệu vàng143 Huy hiệu bạc219 Huy hiệu đồng54 gold badges143 silver badges219 bronze badges
4
Sau nhiều năm, tôi gặp vấn đề tương tự, và sau khi tìm kiếm, tôi đã đến đây một lần nữa]]
Đây là giải pháp, làm việc cho tôi:
$file = "somefile.xlsx";
// define file $mime type here
ob_end_clean[]; // this is solution
header['Content-Description: File Transfer'];
header['Content-Type: ' . $mime];
header["Content-Transfer-Encoding: Binary"];
header["Content-disposition: attachment; filename=\"" . basename[$file] . "\""];
header['Content-Transfer-Encoding: binary'];
header['Expires: 0'];
header['Cache-Control: must-revalidate'];
header['Pragma: public'];
readfile[$file];
Đã trả lời ngày 28 tháng 1 năm 2020 lúc 8:49Jan 28, 2020 at 8:49
Oto Shavadzeoto ShavadzeOto Shavadze
38.6K54 Huy hiệu vàng143 Huy hiệu bạc219 Huy hiệu đồng54 gold badges143 silver badges219 bronze badges
0
Sau nhiều năm, tôi gặp vấn đề tương tự, và sau khi tìm kiếm, tôi đã đến đây một lần nữa]]
Đây là giải pháp, làm việc cho tôi:
Đã trả lời ngày 28 tháng 1 năm 2020 lúc 8:49
ob_clean[];
flush[];
$file = "somefile.xlsx";
header['Content-Description: File Transfer'];
header['Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'];
header['Content-Disposition: attachment; filename='.basename[$file]];
header['Content-Transfer-Encoding: binary'];
header['Expires: 0'];
header['Cache-Control: must-revalidate'];
header['Pragma: public'];
header['Content-Length: ' . filesize[$file]];
readfile[$file];
Bạn phải sử dụng mã này ở giữa một số tệp khác.Jul 19, 2016 at 12:20
1
Remove:
ob_clean[];
flush[];
Vấn đề với các tiêu đề là chúng cần được đặt đầu tiên trên một trang. Chúng sẽ không hoạt động nếu bạn thậm chí có 1 không gian đơn lẻ trước họ. Vì vậy, bạn cần ob_clean [] [làm sạch bộ đệm] trước khi bạn đặt tiêu đề
exit[];
Thử
Đã trả lời ngày 19 tháng 7 năm 2016 lúc 12:20
Thêm ở cuối mã:Apr 5, 2015 at 11:18
Vấn đề là flush [] cũng sẽ ném vào nội dung tệp *.xlsx của bạn một số rác mà nó có trong đó và điều đó sẽ xác nhận tệp của bạn, ngay cả khi bạn sử dụng ob_clean [];besciualex
Để hiểu rõ hơn, hãy truy cập php.net và đọc sự khác biệt giữa Flush [], ob_flush [] và thấy rằng bạn thậm chí không cần chúng trong trường hợp đầu tiên. Do đó, bạn cũng sẽ không cần ob_clean [].1 gold badge14 silver badges20 bronze badges
1
Đã trả lời ngày 5 tháng 4 năm 2015 lúc 11:18
header['Content-Description: File Transfer'];
header["Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"];
header["Content-Disposition: attachment; filename=\"".basename[$fileLocation]."\""];
header["Content-Transfer-Encoding: binary"];
header["Expires: 0"];
header["Pragma: public"];
header["Cache-Control: must-revalidate, post-check=0, pre-check=0"];
header['Content-Length: ' . filesize[$fileLocation]]; //Remove
ob_clean[];
flush[];
readfile[$fileLocation];
BesciualExbesciualexAug 15, 2015 at 2:10