Solved

Only getting partial file, need full

Posted on 2003-11-11
3
283 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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…
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…
The viewer will learn how to dynamically set the form action using jQuery.

630 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