Solved

feof doesn't work on remote fgets call

Posted on 2003-11-05
2
401 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 125 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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

696 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