fputs()出现异常怎么办?
页面提示:
Assembly code<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->Warning: fputs() expects parameter 1 to be resource, boolean given in x.php on line 100Warning: feof() expects parameter 1 to be resource, boolean given in x.php on line 101Warning: fgets() expects parameter 1 to be resource, boolean given in x.php on line 102
提示的时候就是这三行不停的循环输出,大概有几十上百行吧,页面卡死,我直接关掉浏览器
也不是每次都如此,大部分都是正常的,只是偶尔碰到连接的时候会出现这样的错误异常
x.php文件:
PHP code<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> $data = ""; $text = "xawasdf"; $server = "abc.com"; $fp = fsockopen($server, 43, $errNo, $errStr, 10); fputs($fp, $text. "\r\n"); //100行 while (!feof($fp)) { //101行 $data .= fgets($fp, 128); //102行 } fclose($fp);
请问这几行代码有没有修改的可能?
------解决方案--------------------$fp = fsockopen($server, 43, $errNo, $errStr, 10);
失败
应加上排错代码
------解决方案--------------------fsockopen根本就没有打开abc.com
------解决方案--------------------fsockopen() returns a file pointer which may be used together with the other file functions (such as fgets(), fgetss(), fwrite(), fclose(), and feof()). If the call fails, it will return FALSE
这么写 = =
$data = "";
$text = "xawasdf";
$server = "abc.com";
$fp = fsockopen($server, 43, $errNo, $errStr, 10);
if($fp)
{
fputs($fp, $text. "\r\n"); //100行
while (!feof($fp)) { //101行
$data .= fgets($fp, 128); //102行
}
fclose($fp);
}
------解决方案--------------------你fputs参数传成了空句柄,原因是fopen失败造成返回内容(fputs的参数)异常