note 63740 deleted from function.file-get-contents by nlopess
| From: | nlopess@php.net | Date: | Sun, 16 Apr 2006 22:34:16 +0000 |
| Subject: | note 63740 deleted from function.file-get-contents by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-108293@lists.php.net to get a copy of this message | ||
Note Submitter: php dot spam at phihag dot de
----
To dan dot marra at gmail dot com:
Your function may cause undesired effects if the file you are reading in is emtpy: It should return
""(String) but, ($contents = array()) evaluates to false, resulting in the wrong value
false(boolean). Furthermore, your version uses a temporary array(double memory expense!) which is
rather slow. Testing (with a 10M random file on a RAM disk) shows your version is approx. 8 times
slower (!) than the built-in. The following version takes only double the time of the built-in one:
<?php
if(!function_exists("file_get_contents"))
{
function file_get_contents($filename) {
$fp = fopen($filename, "rb");
if (!$fp) {
return false;
}
$contents = "";
while (! feof($fp)) {
$contents .= fread($fp, 4096);
}
return $contents;
}
}
?>
However, if you know the file size or can determine it with low costs, substitute 4096 with the file
size.
Generally, you may also change this constant; it should be a "nice" buffer size on your
platform and slightly bigger than the size of the biggest expected file.