PhpSpreadsheet 返回文件而不是保存它

Bra*_*bor 8 php phpspreadsheet

我已经生成了 xmlx 文件,我可以保存它并通过以下方式提供给用户:

$writer->save('hello world.xlsx');
header('Location: hello world.xlsx');
Run Code Online (Sandbox Code Playgroud)

但是,该文件仍保留在硬盘驱动器上。我需要摆脱它,因为它是一个安全威胁。

我尝试取消链接文件

unlink('hello world.xlsx');
Run Code Online (Sandbox Code Playgroud)

但这会过早地删除文件,因此用户无法访问它。如果它可以与 unlink 一起工作,那么我需要确保该文件将被删除(所以正确使用等等die();)

编辑: 这不仅仅是出于安全原因。提供程序不允许保存文件,因此这是唯一的方法。

Art*_*lov 14

用 php://output

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="file.xls"');
$writer->save("php://output");
Run Code Online (Sandbox Code Playgroud)

  • 在末尾添加“exit();”,这样它就不会下载页面的其余部分 (2认同)