在PHP中实现文件下载的响应可以通过以下步骤实现:
header()
函数设置响应头,包括Content-Type和Content-Disposition。<?php
$file = 'path/to/your/file.pdf';
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
readfile()
函数将文件内容输出到响应流中。readfile($file);
<?php
$file = 'path/to/your/file.pdf';
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
readfile($file);
通过上述步骤,用户访问该PHP页面时将会触发文件下载,而不是在浏览器中打开文件。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:在Java Web中如何实现文件下载