?
Solved

feof doesn't work on remote fgets call

Posted on 2003-11-05
2
Medium Priority
?
415 Views
Last Modified: 2008-01-09
The following code snippet works great, with one slight problem....

if(!($File=@fopen($url,"r")))  // $url = http://www.somedomain.com/somepage.htm?var1=var
{ echo "couldn't read $url "; exit; }

while (!feof($File)) {
 $Line.=fgets($File,255);
}
fclose($File);

The page I'm retrieving is just a plain html table with roughly 20 rows, but the last few characters are always missing. I've tried various line sizes, but still get the same results.  According to a note in the PHP manual, you can't look for feof on a remote file, which seems to be the cause of the missing characters.  

How could the above snippet be re-written to not use feof, and hopefully, not drop the last few characters?
0
Comment
Question by:dewed
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 13

Accepted Solution

by:
lozloz earned 375 total points
ID: 9689951
hi,

why not swap

while (!feof($File)) {
 $Line.=fgets($File,255);
}

with:

$contents = "";
do {
    $data = fread($File, 8192);
    if (strlen($data) == 0) {
        break;
    }
    $contents .= $data;
} while(true);

loz
0
 

Author Comment

by:dewed
ID: 9690798
Im a bit embarresed here. I tried lozloz solution and was amazed that it didn't give me different results...
Turns out the whole problem was just below the code posted above...

$start_position=strpos($Line, $start);
$end_position=strpos($Line, $end)+strlen($end);
$length=$end_position-$start_position;
$Line=substr($Line, $start_position, $length);

because $end did not equal any part $Line , the last line was dropped. I surrounded that block with an if, so if $url had a particular substring, the start/end block above wasn't executed.. it now works perfectly with either my or lozoz's code.
0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question