Opening Text Steam in PHP

I'm glad I read the documentation on fread() method. Now I understand why this method behaves in such way.

The old way (before PHP 5) of handling remote (non file) data stream. What and why is 8192 ? That is the length in byte of the chunk size, which by default is 8192 bytes.
<?php
$handle = fopen("http://www.google.com/", "rb");
$contents = '';
while (!feof($handle)) {
  $contents .= fread($handle, 8192);
}
fclose($handle);
?>

The new way (from PHP 5 onwards) without the need to specific chunk size.
<?php
$handle = fopen("http://www.google.com/", "rb");
$contents = stream_get_contents($handle);
fclose($handle);
?>

No comments:

Post a Comment