Solved

feof doesn't work on remote fgets call

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
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 …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

757 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

22 Experts available now in Live!

Get 1:1 Help Now