2011-10-09 135 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指令。 –

相關問題