while(!feof($ handle)) - 警告信息的错误代码

时间:2013-07-21 20:19:14

标签: php

http://www.example.com/pageView?test=111&test2=999 - >数据加载和工作..

189 <?php
190 $handle = @fopen("http://www.example.com". htmlspecialchars($_GET["test"]) ."/".     htmlspecialchars($_GET["test2"]) .".txt", "r");
191    while (!feof($handle))
192 {
193 $buffer = fgets($handle, 4096);
194
195 if (strlen(trim($buffer)) > 0){
196
197 list($a,$b,$c,$d,$e,$f,$g,$h,$i,$j)=explode(",",$buffer);
198
199 $items[] = array('col1' => $a,'col2' => $b,'col3' => $c,'col4' => $d,'col5' => $e,'col6' => $f,'col7' => $g,'col8' => $h,'col9' => $i,'col10' => $j);

200 }
201 }
202 ?>

http://www.example.com/pageView?test&test2= - &gt;没有加载..这是正常的但这个错误在页面中..

Warning: feof(): supplied argument is not a valid stream resource in /home/---/public_html/---/---.php on line 191

Warning: fgets(): supplied argument is not a valid stream resource in /home/---/public_html/---/---.php on line 193

如何显示警告信而不是错误页?

1 个答案:

答案 0 :(得分:1)

检查fopen的返回值。如果在阅读过程中出现错误,则会将$ handle等于false,但您需要将“stream resource”传递给eof

if($handle === false) {
    print 'Err found';
}
else {
   while(!eof($handle)) ...
}