file_get_contents function not working in version of php
Moderator: General Moderators
-
mccommunity
- Forum Commoner
- Posts: 62
- Joined: Mon Oct 07, 2002 8:55 am
file_get_contents function not working in version of php
I was using file_get_contents ( $buff = file_get_contents($genericfile); ) to grab the contents of a generic file and putting it into a string. I had this up and running fine but they have decided to regress to an older version of php which does not support this. So now my scripts are not working that rely on this function. Is there an identical function or does someone have one written. Any help is appreciated.
Code: Select all
<?php
function get_contents($path, $chunkSize=4096)
{
$fd = @fopen($path, "rb");
if ($fd === false)
return false;
$contents = '';
while(!feof($fd))
$contents .= fread($fd, $chunkSize);
fclose($fd);
return $contents;
}
?>For PHP versions equal to or greater than 4.3.0, use the file_get_contents() function.
For earlier PHP versions use the file() function in conjunction with the implode() function.
The file_get_contents() function is identical to the file() function, except that file_get_contents() returns the file in a string, while file() returns the file in a array, thus the need for implode() usage. Note file_get_contents() is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by your OS to enhance performance.
Code: Select all
<?php
$tag = 'http://example.com/file.php';
$get = file_get_contents($tag) or die('Could not read file!');
echo $get;
?>Code: Select all
<?php
$tag = 'http://example.com/file.php';
$get = implode('', file($tag)) or die('Could not read file!');
echo $get;
?>