Solved

feof doesn't work on remote fgets call

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now