note 63740 added to function.file-get-contents
| From: | php dot spam at phihag dot de | Date: | Wed, 29 Mar 2006 19:04:39 +0000 |
| Subject: | note 63740 added to function.file-get-contents | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-106966@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 212.124.37.9
Probable Submitter: 80.130.132.236
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.file-get-contents.php
Edit -- http://master.php.net/note/edit/63740
Del: integrated -- http://master.php.net/note/delete/63740/integrated
Del: useless -- http://master.php.net/note/delete/63740/useless
Del: bad code -- http://master.php.net/note/delete/63740/bad+code
Del: spam -- http://master.php.net/note/delete/63740/spam
Del: non-english -- http://master.php.net/note/delete/63740/non-english
Del: in docs -- http://master.php.net/note/delete/63740/in+docs
Del: other reasons-- http://master.php.net/note/delete/63740
Reject -- http://master.php.net/note/reject/63740
Search -- http://master.php.net/manage/user-notes.php