我在磁盘上有一个 pdf 文件,当他们向 php 脚本发出请求时我需要将其发送给用户,执行此操作的最佳方法是什么?
最佳答案
假设它在服务器上:
readfile() — Outputs a file
注意:只是写
readfile($file);
不会工作。这将使客户端永远等待响应。您需要定义 header 以使其按预期方式工作。 See this example from the official PHP manual :
<?php
$file = 'monkey.gif';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
?>
关于PHP - 向用户发送文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2882472/