Link to home
Start Free TrialLog in
Avatar of ChrisAndrews
ChrisAndrews

asked on

Only getting partial file, need full


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
Avatar of lozloz
lozloz

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
ASKER CERTIFIED SOLUTION
Avatar of shmert
shmert

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ChrisAndrews

ASKER


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

Chris