Computing.Net > Forums > Web Development > PHP fread() issue with remote files

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

PHP fread() issue with remote files

Reply to Message Icon

Name: Dr. Nick
Date: September 15, 2004 at 23:23:59 Pacific
OS: WinXP Pro SP1a
CPU/Ram: P4 2.0Ghz / 1024MB
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: Dr. Nick
Date: September 15, 2004 at 23:24:41 Pacific
Reply:

Oops, ignore that <table> bit... copied more than I meant to.


0

Response Number 2
Name: Dr. Nick
Date: September 15, 2004 at 23:27:20 Pacific
Reply:

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.


0

Response Number 3
Name: Laler
Date: September 16, 2004 at 02:00:19 Pacific
Reply:

/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


0

Response Number 4
Name: Dr. Nick
Date: September 16, 2004 at 02:14:57 Pacific
Reply:

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.


0

Response Number 5
Name: SN
Date: September 16, 2004 at 06:43:56 Pacific
Reply:

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

-SN


0

Related Posts

See More



Response Number 6
Name: Dr. Nick
Date: September 16, 2004 at 12:59:30 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: PHP fread() issue with remote files

PHP/IIS Issue www.computing.net/answers/webdevel/phpiis-issue/2111.html

PHP upload file to remote host www.computing.net/answers/webdevel/php-upload-file-to-remote-host/2763.html

html input type=file and php www.computing.net/answers/webdevel/html-input-typefile-and-php/214.html