显示未正确显示PHP的pdf

显示未正确显示PHP的pdf,php,pdf,Php,Pdf,我在存储服务器上有一些pdf文件,我想向用户显示它们,而不向他们显示真实的文件路径,我想用PHP实现这一点 我试着这样做: $file = 'https://storage.server_test.com/agc/catalogs/catalog1.pdf'; $filename = 'catalog.pdf'; header('Content-type: application/pdf'); header('Content-Disposition: inline; filename="'

我在存储服务器上有一些pdf文件,我想向用户显示它们,而不向他们显示真实的文件路径,我想用PHP实现这一点

我试着这样做:

$file = 'https://storage.server_test.com/agc/catalogs/catalog1.pdf';
$filename = 'catalog.pdf'; 

header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');

@readfile($file);
但是pdf根本没有显示,我得到一个错误:
这个pdf文档可能没有正确显示。


我做错了什么?

您是否尝试过使用html
object
标记嵌入它

<object data="https://storage.server_test.com/agc/catalogs/catalog1.pdf" type="application/pdf">
    <embed src="https://storage.server_test.com/agc/catalogs/catalog1.pdf" type="application/pdf" />
</object>
最后,如果
@readfile($file)
不起作用,请尝试:

echo @file_get_contents($file);

在我自己在当地尝试过之后,我想我成功地让它工作了。试试这个:

<?php

$remote_pdf = 'http://www.saronicferries.gr/content/pdfFiles/sample.pdf';
$remote_file_name = end(explode('/', $remote_pdf));
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="'. $remote_file_name .'"');
header('Content-Transfer-Encoding: binary');
readfile($remote_pdf);

?>

您是否对本地文件进行了相同的尝试?没有,因为文件不是本地存储的。它们存储在存储服务器上,因此我只有到它们的绝对路径。您是否检查了
filesize
readfile
对非本地文件是否能正常工作?正如我所说的,我的示例不起作用,这就是我发布此问题的原因。你想建议它应该工作吗?不。我建议你尝试
var_dump(filesize($file),readfile($file))
(没有沉默操作符!)正如我说的,我想从php做这件事,因为我不想让用户看到文件的真实路径。答案更新。尝试那种读取远程内容的页眉或alt方式。我尝试了最后一行,得到了如下结果:%PDF-1.6%–ãÓ3948 0 obj stream hÞTʱÃÐu qèRˆwWShR×@。。。。打印在屏幕上,现在可以工作了。谢谢你认为这个问题对其他人也有用吗?还有一件事,标签上写着:untitled-pdf。我可以重新命名吗?
<?php

$remote_pdf = 'http://www.saronicferries.gr/content/pdfFiles/sample.pdf';
$remote_file_name = end(explode('/', $remote_pdf));
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="'. $remote_file_name .'"');
header('Content-Transfer-Encoding: binary');
readfile($remote_pdf);

?>