我已经生成了 xmlx 文件,我可以保存它并通过以下方式提供给用户:
$writer->save('hello world.xlsx');
header('Location: hello world.xlsx');
但是,该文件仍保留在硬盘驱动器上。我需要摆脱它,因为它是一个安全威胁。
我尝试取消链接文件
unlink('hello world.xlsx');
但这会过早地删除文件,因此用户无法访问它。
如果它可以与 unlink 一起工作,那么我需要确保文件将被删除(所以正确使用
die();
等)编辑:
这不仅仅是出于安全原因。提供程序不允许保存文件,因此这是唯一的方法。
最佳答案
使用 php://output
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="file.xls"');
$writer->save("php://output");
关于PhpSpreadsheet 返回文件而不是保存它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50319493/