Solved

Only getting partial file, need full

Posted on 2003-11-11
3
271 Views
Last Modified: 2008-02-07

This used to work ok, for some reason now I only get part of the file being read.  Not sure if something in this code doesn't work with the newer php update or what?  I need the full page read, and adjusting the size on fread doesn't seem to make a difference.

Code:

<?
$fileloc = "http://www.andrews.com/index.html";
$fp = fopen($fileloc,"r");
$content = fread($fp, 200000);
echo $content;
?>

echo $content only returns a small portion of page, not full page.  I need the whole page read.

Thanks for any assistance :)

Chris
0
Comment
Question by:ChrisAndrews
3 Comments
 
LVL 13

Expert Comment

by:lozloz
ID: 9725266
hi,

$content = fread($fp, 200000);

this line means read the first 200kb of the file and that's it. if the file is on a remote server, you want something more like:

<?
$fileloc = "http://www.andrews.com/index.html";
$fp = fopen($fileloc,"rb");
$content = "";
do {
    $data = fread($fp, 8192);
    if (strlen($data) == 0) {
        break;
    }
    $content .= $data;
} while(true);
echo $content;
fclose ($fp);
?>

but if it's a local file, you can just use:

<?
$fileloc = "http://www.andrews.com/index.html";
$fp = fopen($fileloc,"rb");
$content = fread ($fp, filesize ($fileloc));
echo $content;
fclose ($fp);
?>

you may need to reference a local file in a relative way to be able to use the filesize function though

loz
0
 
LVL 11

Accepted Solution

by:
shmert earned 125 total points
ID: 9732283
If you're using php >= 4.3 then use
file_get_contents($fileloc)
as that will give you the best performance, and will always return the complete file.
0
 

Author Comment

by:ChrisAndrews
ID: 9734725

Thank you, I tried both and the file_get_contents has worked best :)

Chris
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
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…

763 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