fiel_get_contents 获取本地php文件返回空白

fiel_get_contents 该函数访问远程文件无问题;今天的问题是获取本机的文件,返回值为空。

例如:该域名 www.hello.com 下有两个文件A.php 和 B.php;即A和B两个文件都是在同一服务器里面。

A.php 获取 b.php 返回的内容:fiel_get_contents('http://www.hello.com/B.php');

B.php 输出 内容:echo '123456789';

返回内容是空白的,解决办法:将网址:www.hello.com 改为 本服务器的 ip 地址;或者,改为 localhost 即可解决问题。

 

如果,使用相对路径的话,例如:fiel_get_contents('../B.php'); 获取到的是源文件代码,而不是经过服务器处理输出的实际内容。

 

题外笔记:

如果,php文件内使用:header('Content-type: image/jpeg'); 同时,又使用 file_get_contents 模拟发送头部信息 是会有冲突的

$context = stream_context_create($hdrs);

echo file_get_contents('http://www.hello.com/123.jpg', 0, $context); 

但是,不发送头部信息,即这样用 echo file_get_contents('http://www.hello.com/123.jpg');  就没有问题。

两者是有区别的。

posted @ 2017-08-16 09:44  stma  阅读(1205)  评论(0)    收藏  举报