如何处理 PHP 中 file_get_contents() 函数的警告? [英] How can I handle the warning of file_get_contents() function in PHP?
问题描述
我写了这样一段 PHP 代码
I wrote a PHP code like this
$site="http://www.google.com";
$content = file_get_content($site);
echo $content;
但是当我从 $site
中删除http://"时,我收到以下警告:
But when I remove "http://" from $site
I get the following warning:
警告:file_get_contents(www.google.com)[function.file-get-contents]:失败打开流:
Warning: file_get_contents(www.google.com) [function.file-get-contents]: failed to open stream:
我尝试了 try
和 catch
但它没有用.
I tried try
and catch
but it didn't work.
推荐答案
第一步:检查返回码:if($content === FALSE) {//在这里处理错误... }
第 2 步:通过放置一个错误控制运算符来抑制警告(即 @
)在对 file_get_contents() 的调用之前:$content = @file_get_contents($site);
Step 2: suppress the warning by putting an error control operator (i.e. @
) in front of the call to file_get_contents():
$content = @file_get_contents($site);
这篇关于如何处理 PHP 中 file_get_contents() 函数的警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!