Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Name: Dr. Nick
Running PHP 4.3.8 on WinXP Pro.
Not a great subject for the thread, but here's my problem.
I'm trying to open a remote webpage using
$fp = fopen("http://the_remote_url", "r");
then reading in the entire page using:
$content = fread($fp, 999999);
According to everything I've seen this should work. My problem is that I'm only able to read in exactly 16131 bytes. It's the same every time no matter what the remote file is, even if I'm hosting it from my own machine (ie: 'http://localhost/file.txt').If I try reading just a flat file from a local directory (ie: 'file.txt') it reads the entire thing with no problem.
Does anyone have any ideas why it would only read in 16131 bytes, then just stop? No errors or warnings coming from PHP.
Full code:
<table border="0" width="500">
<?if (!($fp = fopen("http://computing.net", 'r')))
{
echo 'Error opening URL.';
exit;
}$contents = fread($fp, 16131);
fclose($fp);?>
</tabl>
Thanks.

I'm way too tired.
Also, inside the code block where it says fread($fp, 16131) that should be fread($fp, 999999). Was testing to find out if it really was limiting it to 16131 bytes and forgot to change it back.
Thanks again.

/snip
When reading from network streams or pipes, such as those returned when reading remote files or from popen() and fsockopen(), reading will stop after a packet is available. This means that you should collect the data together in chunks as shown in the example below.<?php
$handle = fopen("http://www.example.com/", "rb");
$contents = '';
while (!feof($handle)) {
$contents .= fread($handle, 8192);
}
fclose($handle);
?>/snipped from php manual
:)
^o^
are you in Asia? do you watch Animax Asia? Please Vote

Hmmm.
That did work, thanks! I swear I read over fread() on php.net, but I guess not.
Had a slew of websites that said to use a big number (in the millions) to get all the content. Don't want to look for them now, but this was one.
Thanks again.

You probably considered this already, but you may want to have a look at file_get_contents().
-SN

Yeah I looked at that as well. It worked very well at reading in the entire page, but it's somewhat limiting because you can't specify how much to read in.
My goal is to be able to read in from a page until I've got what I need. I'd rather not pull more from the remote server than I need to.
Thanks.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |