Skip to content

file_get_contents function for PHP 4

One of the functions added in PHP 5 is file_get_contents. This function allows you to just pass a file name and it will return the entire contents of the file. That’s all well and good if you’re using PHP5, but PHP4 is still very common.

Needing the functionality of file_get_contents, and growing tired of writing out the fopen, fread, fclose set needed to perform the same function in PHP4, I wrote the following function. It’s named a little differently just so that it’s not the same function name and is usable whether the target server is PHP4 or PHP5.

The function will use file_get_contents on a PHP5 server and drop to fopen, fread, and fclose on PHP4 servers.

function get_file_contents($filename)
/* Returns the contents of file name passed
*/
{
if (!function_exists('file_get_contents'))
{
$fhandle = fopen($filename, "r");
$fcontents = fread($fhandle, filesize($filename));
fclose($fhandle);
}
else
{
$fcontents = file_get_contents($filename);
}
return $fcontents;
}
Published inProgramming

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *