我写了这样的PHP代码
$site="http://www.google.com";
$content = file_get_content($site);
echo $content;
但是当我从$site中删除“http://”时,我得到以下警告:
警告: file_get_contents (www.google.com) (函数。file-get-contents]:失败 打开水流:
我试过,试过,但没成功。
我写了这样的PHP代码
$site="http://www.google.com";
$content = file_get_content($site);
echo $content;
但是当我从$site中删除“http://”时,我得到以下警告:
警告: file_get_contents (www.google.com) (函数。file-get-contents]:失败 打开水流:
我试过,试过,但没成功。
当前回答
您可以使用这个脚本
$url = @file_get_contents("http://www.itreb.info");
if ($url) {
// if url is true execute this
echo $url;
} else {
// if not exceute this
echo "connection error";
}
其他回答
最好的方法是设置自己的错误和异常处理程序,这些处理程序将做一些有用的事情,如将其记录在文件中或通过电子邮件发送关键的错误和异常处理程序。 http://www.php.net/set_error_handler
第一步:检查返回代码:if($content === FALSE){//处理错误这里…}
第二步:通过在file_get_contents()调用前放置一个错误控制操作符(即@)来抑制警告: $content = @file_get_contents($site);
if (!file_get_contents($data)) {
exit('<h1>ERROR MESSAGE</h1>');
} else {
return file_get_contents($data);
}
在使用file_get_contents()之前,应该使用file_exists()函数。 这样可以避免php警告。
$file = "path/to/file";
if(file_exists($file)){
$content = file_get_contents($file);
}
因为PHP 4使用error_reporting():
$site="http://www.google.com";
$old_error_reporting = error_reporting(E_ALL ^ E_WARNING);
$content = file_get_content($site);
error_reporting($old_error_reporting);
if ($content === FALSE) {
echo "Error getting '$site'";
} else {
echo $content;
}