Sal*_*man 5 php stream raw-data
我正在从原始二进制数据创建一个PDF文件,它工作得很好,但由于我在PHP文件中定义的标题,它会提示用户"保存"文件或"打开".有什么方法可以将文件保存在本地某处的本地服务器上http://localhost/pdf吗?
以下是我在页面中定义的标题
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: application/pdf");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Transfer-Encoding: binary");
Run Code Online (Sandbox Code Playgroud)
Sim*_*mon 10
如果您想将文件保存在服务器上而不是让访问者下载它,您将不需要标题.标题用于告诉客户端您发送的内容,在这种情况下没有任何内容(尽管您可能会显示链接到新创建的PDF或其他内容的页面).
因此,只需使用诸如file_put_contents在本地存储文件的功能,最终让您的Web服务器处理文件传输和HTTP标头.
// Let's say you have a function `generate_pdf()` which creates the PDF,
// and a variable $pdf_data where the file contents are stored upon creation
$pdf_data = generate_pdf();
// And a path where the file will be created
$path = '/path/to/your/www/root/public_html/newly_created_file.pdf';
// Then just save it like this
file_put_contents( $path, $pdf_data );
// Proceed in whatever way suitable, giving the user feedback if needed
// Eg. providing a download link to http://localhost/newly_created_file.pdf
Run Code Online (Sandbox Code Playgroud)