2011-10-09 61 views
0

Possible Duplicate:
including php file from another server with php为什么我得到1而不是所需的文件?

我想有我自己的错误的系统,而不是具有PHP错误,这样的例子,我需要从另一台服务器上的文件,并且该服务器目前无法使用

<?php 
require 'http://example.com/file.php' or die ("that host is not available right now"); 
?> 

而是我得到

Warning: require(1) [function.require]: failed to open stream: No such file or directory in C:\xampp\htdocs\index.php on line 5

Fatal error: require() [function.require]: Failed opening required '1' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\index.php on line 5

+1

如果您不是'example.com'的管理员,这是一个非常糟糕的主意。 – Dennis

+0

@Pablo不,这不是'require(1)'的原因。 '1'是这里的问题。 – deceze

回答

6

这是因为require 'foo' or bar()被解释为require ('foo' or bar())'foo' or bar()等于true,即1。如果你想要把它写这样的,使用不同的括号:

(require 'http://example.com/file.php') or die ("that host is not available right now"); 

,你并不需要在die都在这里,因为require已经将暂停程序执行,如果需要的文件无法加载。只要require 'http://example.com/file.php';将会很好。是否应该通过网络实际加载外部PHP文件是另一回事(提示:可能不是)。

+0

(回应删除的评论:)不,因为'require'已经停止了程序的执行! *失败后,没有任何*需求。如果您想要自定义错误处理,请改为重置错误处理程序。 – deceze

+0

对于愚蠢的问题,我很抱歉(我的猜测)我该如何做? – elibyy

+0

看看http://php.net/manual/en/function.set-error-handler.php – deceze

3

问题是运营商的优先级。 PHP包含“或”比较结果(true)。尝试删除此。

include('http://...') or die('error...'); 

它会工作。

+0

如果我删除或我得到其他错误 – elibyy

+0

如果你想定制检查你的错误,使用包含功能,而不是require语句。如果您遇到错误,请阅读其他答案。打开allow_url_include指令。 –

相关问题