通常,浏览器显示图像和pdf文件而不将它们嵌入到html中。我需要一些代码才能使这些文件不在浏览器中显示,但可以像doc文件那样下载。
请帮我解决这个问题。
答案 0 :(得分:5)
这不取决于您,取决于浏览器。
但是,您可以通过设置content-disposition
标题来建议如何处理它...
header("Content-Disposition: attachment; filename=\"yourfilename.pdf\"");
阅读header()
函数上的文档:http://php.net/manual/en/function.header.php
如果不清楚......这适用于PHP文档返回的任何资源。您可能需要readfile()
来执行您要执行的操作。
答案 1 :(得分:2)
设置几个标题:
$filename = ...;
$mime_type = ...; //whichever applicable MIME type
header('Pragma: public');
header('Expires: 0');
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: $mime_type");
header('Content-Length: ' . filesize($filename));
readfile($filename);
答案 2 :(得分:1)
<?php
header('Content-disposition: attachment; filename=myfile.pdf');
header('Content-type: application/pdf');
readfile('myfile.pdf');
?>
答案 3 :(得分:1)
您希望发送内容类型标头以使浏览器下载文件。
如果你不是'动态生成它,你需要先从磁盘上读取它。
$fullPath = "/path/to/file/on/server.pdf";
$fsize = filesize($fullPath);
$content = file_get_contents($fullPath);
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: application/pdf");
header("Content-Disposition: attachment; filename=\"".basename($fullPath)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$fsize);
echo $content;
答案 4 :(得分:0)
试试这个:
$file = 'youfile.fileextention';
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;