将文件的内容回显给用户,但使其充当下载

jax*_*jax 3 php

我有一个脚本,我获取文件的内容然后将其回显到屏幕,问题是它实际上将二进制文件回显到页面,我想要的是如果它像下载一样下载对话框将显示.

我该怎么做到这一点?

Jim*_* W. 5

从PHP header()手册:


// We'll be outputting a PDF
header('Content-type: application/pdf');

// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');

// The PDF source is in original.pdf
readfile('original.pdf');
Run Code Online (Sandbox Code Playgroud)

更改内容类型和文件名.您可以通过file_get_contents使用readfile,但是要么工作要么应该工作.