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
Solved

Only getting partial file, need full

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

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.
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

856 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