如何处理 PHP 中 file_get_contents() 函数的警告? [英] How can I handle the warning of file_get_contents() function in PHP?

查看:32
本文介绍了如何处理 PHP 中 file_get_contents() 函数的警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这样一段 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:

我尝试了 trycatch 但它没有用.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆