在php下载pdf文件代码

时间:2012-12-12 05:05:28

标签: php pdf

$path = BASE_URL."/pdf/"; 
$filename= $path.basename($_GET['download_file']);

header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-disposition: attachment; 
filename='.basename($filename));
header("Content-Type: application/pdf");
header("Content-Transfer-Encoding: binary");
header('Content-Length: '. filesize($filename));
readfile($filename);
exit;

此代码有效,但打开下载的pdf时我得到Error in reading pdf file。在上面的代码中,我从位置http://localhost//eec//pdf/CV_Prabin Mishra.pdf

获取文件

2 个答案:

答案 0 :(得分:1)

BASE_URL最后可能有一个斜线,所以你不需要额外的一个:

$path = BASE_URL."pdf/"; 

答案 1 :(得分:1)

我没有看到你在哪里设置$ filename的值,你在$ fullPath中设置PDF的位置,但是然后使用$ filename来读取它。我认为代码应该是

$path = BASE_URL."/pdf/"; 
$fullPath = $path.basename($_GET['download_file']);

header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-disposition: attachment; 
filename='.basename($fullPath));
header("Content-Type: application/pdf");
header("Content-Transfer-Encoding: binary");
header('Content-Length: '. filesize($fullPath));
readfile($fullPath);
exit;