php中的强制文件下载
我已经构建了一个简单的文件管理器,用户可以下载任何类型的文件,例如pdf,word或gif文件。我希望所有人都下载文件,而不是在浏览器中查看它。上传的文件名存储在数据库中。php中的强制文件下载
回答:
可以使用“内容处置”标题为:
header("Content-Disposition: attachment");
的PHP manual提供了用于一个很好的例子。
回答:
由浏览器发送文件强制下载之前通常设置Content-Disposition
到attachment
。
你要么需要配置Web服务器,为文件提供此头或自己通过PHP给他们,像以前一样发送一个特定的头:
header('Content-Disposition: attachment; filename=your_file_name.pdf');
要注意的是第一个解决方案是为你赢得更好不会因为脚本运行时间过长而导致下载量下降(您也可以改变它)。从TCPDF库采取
回答:
<?php // 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');
?>
http://php.net/manual/en/function.header.php
回答:
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT\n"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Content-type: application/pdf;\n");
$len = filesize($filename);
header("Content-Length: $len;\n");
header("Content-Disposition: attachment; filename=\"downfile.pdf\";\n\n");
echo readfile($filename)
回答:
源代码
// download PDF as file if (ob_get_contents()) {
$this->Error('Some data has already been output, can\'t send PDF file');
}
header('Content-Description: File Transfer');
if (headers_sent()) {
$this->Error('Some data has already been output to browser, can\'t send PDF file');
}
header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
// force download dialog
if (strpos(php_sapi_name(), 'cgi') === false) {
header('Content-Type: application/force-download');
header('Content-Type: application/octet-stream', false);
header('Content-Type: application/download', false);
header('Content-Type: application/pdf', false);
} else {
header('Content-Type: application/pdf');
}
// use the Content-Disposition header to supply a recommended filename
header('Content-Disposition: attachment; filename="'.basename($name).'";');
header('Content-Transfer-Encoding: binary');
$this->sendOutputData($this->getBuffer(), $this->bufferlen);
break;
反正最重要的部分是
header('Content-Disposition: attachment; filename="'.basename($name).'";');
并注意鳍没有它,它不会工作
以上是 php中的强制文件下载 的全部内容, 来源链接: utcz.com/qa/266399.html