我有一个pdfreader.php和其他4个php文件,pdfreader.php位于其他4个php文件之外的另一个目录中。
我想使用pdfreader.php中的功能,它可以让人们阅读pdf文件,但所有其他pdf文件都有其他功能,包括其他文件。因为我只能在$file变量中执行一个文件。如果有另一个解决方案,我们也将不胜感激。
我在pdfreader.php中的代码:
function test(){
$file = '../files/testtt.pdf';
$filename = 'testtt.pdf';
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
@readfile($file);
}
在我的其他php文件中,我有:
include '../includes/pdfreader.php';
test();
但当我尝试加载时,它说:加载PDF文档失败。
当我在没有函数的情况下进行操作时,它是有效的。我先用它和这个代码(没有功能):
echo "<a href='../includes/pdfreader.php' class='btndwn blue'> Check </a>";
如果可能的话,我也想用一个按钮来做。我希望有人能帮助我。
提前谢谢。
这两个文件可能在不同的目录中。当其他文件调用test()时,PHP解析相对于调用方的路径。你可以试试:
$file = __DIR__ . '/../files/testtt.pdf';
我认为路径错误。如何构建文件夹?我猜文件夹files
在文件夹includes
中。所以pdfreader.php中的路径应该是../includes/files/testtt.pdf
。运行其他文件时,当前路径基于其他文件。因此,当包含pdfreader.php时,其中的路径都是错误的。它将在当前文件夹的父目录中搜索files文件夹。