奇怪的readfile()问题

时间:2012-07-18 12:35:33

标签: php

我的脚本如下:

<?php
header("Content-type:application/pdf");

// It will be called downloaded.pdf
header("Content-Disposition:attachment;filename='test.pdf'");

// The PDF source is in original.pdf
readfile("www.example.com/test.pdf");
?>

现在,如果我将readfile更改为:

// The PDF source is in original.pdf
readfile("test.pdf");

如果我指定了绝对网址readfile("www.example.com/test.pdf");

,它可以正常工作

PDF无法打开。以上似乎只能在本地使用。

有人知道为什么绝对网址不起作用吗?干杯

解决方案:

// The PDF source is in original.pdf 

readfile($_SERVER['DOCUMENT_ROOT']."/test.pdf"); 

此功能仅适用于绝对路径,而不适用于网址。

3 个答案:

答案 0 :(得分:2)

  • 您缺少协议:http://
  • 并检查您的allow_url_fopen设置!
  • 启用错误报告

ini_set('display_errors', true);
error_reporting(E_ALL);

更智能的方法是在本地存储/缓存远程文件,因此您不必每次都下载它。它会减轻目标站点的负担。

答案 1 :(得分:0)

如果已启用fopen包装,则可以将URL用作此函数的文件名。

答案 2 :(得分:0)

尝试这个......,

<?php
header("Content-type:application/pdf");

// It will be called downloaded.pdf
header("Content-Disposition:attachment;filename='test.pdf'");

// The PDF source is in original.pdf
readfile("http://www.example.com/test.pdf");
?>

没有“http://”你无法下载。

相关问题