pdf文件下载的正确PHP标题

pdf文件下载的正确PHP标题,php,pdf,header,Php,Pdf,Header,我真的很难让我的应用程序在用户点击链接时打开pdf 到目前为止,锚定标记重定向到发送以下标题的页面: $filename='./pdf/jobs/pdffile.pdf; $url_download = BASE_URL . RELATIVE_PATH . $filename; header("Content-type:application/pdf"); header("Content-Disposition:inline;filename='$filenam

我真的很难让我的应用程序在用户点击链接时打开pdf

到目前为止,锚定标记重定向到发送以下标题的页面:

$filename='./pdf/jobs/pdffile.pdf;

$url_download = BASE_URL . RELATIVE_PATH . $filename;

header("Content-type:application/pdf");
header("Content-Disposition:inline;filename='$filename");
readfile("downloaded.pdf");

这似乎不起作用,过去有人成功解决过这个问题吗?

您需要定义文件的大小

header('Content-Length: ' . filesize($file));
这句话是错误的:

标题(“内容配置:内联;文件名='$filename”)

你搞乱了配额。

上的示例2显示了你试图实现的目标


您能试试这个吗,
readfile
需要完整的文件路径

        $filename='/pdf/jobs/pdffile.pdf';            
        $url_download = BASE_URL . RELATIVE_PATH . $filename;            

        //header("Content-type:application/pdf");   
        header("Content-type: application/octet-stream");                       
        header("Content-Disposition:inline;filename='".basename($filename)."'");            
        header('Content-Length: ' . filesize($filename));
        header("Cache-control: private"); //use this to open files directly                     
        readfile($filename);

在您的代码中有一些东西需要考虑

首先,正确地编写这些标题。您将永远不会看到任何服务器发送
内容类型:application/pdf
,标题为
内容类型:application/pdf
,空格,首字母大写等

Content Disposition
中的文件名只是文件名,而不是文件的完整路径,虽然我不知道它是否是必需的,但这个名称被包装在
而不是
中。另外,您最后一个
也丢失了

Content-Disposition:inline
表示应该显示文件,而不是下载。请改用
附件

此外,请将文件扩展名设置为大写,以使其与某些移动设备兼容。(更新:非常确定只有黑莓有此问题,但世界已经从这些问题转移到了其他问题,因此这可能不再是一个问题)

尽管如此,您的代码应该更像这样:

<?php

    $filename = './pdf/jobs/pdffile.pdf';

    $fileinfo = pathinfo($filename);
    $sendname = $fileinfo['filename'] . '.' . strtoupper($fileinfo['extension']);

    header('Content-Type: application/pdf');
    header("Content-Disposition: attachment; filename=\"$sendname\"");
    header('Content-Length: ' . filesize($filename));
    readfile($filename);

我最近也遇到了同样的问题,这对我有帮助:

    header('Content-Description: File Transfer'); 
    header('Content-Type: application/octet-stream'); 
    header('Content-Disposition: attachment; filename="FILENAME"'); 
    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("PATH/TO/FILE")); 
    ob_clean(); 
    flush(); 
    readfile(PATH/TO/FILE);      
    exit();

我找到了这个答案

输入错误?请尝试添加一个
'
$filename='./pdf/jobs/pdffile.pdf';
并在这行
标题中(“内容配置:内联;filename='$filename”)
缺少引号。如何/为什么使用
$url\u下载
?不,您不需要给出文件的大小。这也是错误的可能来源。取决于PDF插件版本。给出它是安全的。无法使其正常工作。我正在测试的$file变量是什么?是文件url./PDF/jobs/pdftitle.PDF吗?yes、 它是文件位置…可能你弄乱了路径?尝试给出绝对路径。哈哈,是真的。我提到它只是因为它有相同的示例。加上1作为输出通知。记住删除文件名周围的单引号。如果使用filename='download.pdf',一些浏览器会尝试使用文件名中的引号保存文件。我最近在OSX上遇到过这种情况。
readfile()
对于大文件来说非常糟糕,因为当一些用户想要下载时,每个下载文件缓冲区都在服务器ram上!!!PDF究竟为什么会有HTML标记?你不需要结束标记
?>
。在这种情况下,最好将其删除。可以肯定的是,将头文件提供的文件名用单引号括起来是不正确的正如您所建议的,下载会带来很大的不同。顶部的ob_start();可确保数据损坏注意,将整个文件内容加载到变量中可能会与内存限制发生冲突。这就是为什么
readfile()
是首选解决方案的原因。
$name = 'file.pdf';
//file_get_contents is standard function
$content = file_get_contents($name);
header('Content-Type: application/pdf');
header('Content-Length: '.strlen( $content ));
header('Content-disposition: inline; filename="' . $name . '"');
header('Cache-Control: public, must-revalidate, max-age=0');
header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
echo $content;
    header('Content-Description: File Transfer'); 
    header('Content-Type: application/octet-stream'); 
    header('Content-Disposition: attachment; filename="FILENAME"'); 
    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("PATH/TO/FILE")); 
    ob_clean(); 
    flush(); 
    readfile(PATH/TO/FILE);      
    exit();
header("Content-type:application/pdf");

// It will be called downloaded.pdf thats mean define file name would be show

header("Content-Disposition:attachment;filename=  $fileName  ");

// The PDF source is in original.pdf

readfile($file_url);