Solved

Only getting partial file, need full

Posted on 2003-11-11
3
239 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

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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.
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 …

747 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

16 Experts available now in Live!

Get 1:1 Help Now