以下是代码:
define(ADMIN_ROOT,'/var/www/abc/pqr/web/control/');
$filename = $test_data['test_name'];
$flname = ADMIN_ROOT.'modules/tests/test_pdfs/'.$filename;
header("Content-Type: application/pdf");
header("Cache-Control: no-cache");
header("Accept-Ranges: none");
header("Content-Disposition: attachment; filename=\"" .$flname. ".pdf\"");
目录
上已存在pdf文件/var/www/abc/pqr/web/control/modules/tests/test_pdfs/
此外它还具有所有权限。但是当我尝试下载文件时,它正在下载一些具有相同名称的不同文件,但该文件的格式不是可以打开的。简而言之,不会下载所需的文件。有人可以帮我纠正我的问题吗?
答案 0 :(得分:3)
您必须将文件内容刷新到浏览器,标题不会这样做。
所以:readfile( $flname )
http://php.net/manual/pt_BR/function.readfile.php
答案 1 :(得分:0)
请试试这个
<?php
$filename = $test_data['test_name'];
$file = dirname(__FILE__) . "/modules/tests/test_pdfs/" . $filename;
//$file = "/var/www/htdocs/audio/" . $filename;
if (file_exists($file)) {
header ("Content-type: octet/stream");
header ("Content-disposition: attachment; filename=" . str_replace(" ", "_", basename($file)) . ";");
header ("Content-Length: " . filesize($file));
readfile($file);
die();
}
else {
//ERROR MESSAGE HERE..........
}
?>
请更改此行
header("Content-Type: application/pdf");
到
header ("Content-type: octet/stream");
答案 2 :(得分:0)
You can also use the following code for download any type of file extensions:
<?php
$filename = $test_data['test_name'];
$contenttype = "application/force-download";
header("Content-Type: " . $contenttype);
header("Content-Disposition: attachment; filename=\"" . basename($filename) . "\";");
readfile(ADMIN_ROOT.'modules/tests/test_pdfs/'.$filename);
exit();
?>