PDA

View Full Version : Work-around for file_get_contents?


saifai
08-04-2006, 03:22 AM
I'm getting the following error for a header file in some new software I'm using, which I already know is because of a PHP compatibility problem:
Fatal error: Call to undefined function: file_get_contents()

I was hoping to find a work-around for that. Someone suggested I try using GetContents instead, but I wasn't sure how to make it work.

They had suggested the following code for other software:
function getContents( $filename )
{
if ( function_exists( 'file_get_contents' ) )
{
return file_get_contents( $filename );
}
else
{
$fp = fopen( $filename, 'r' );
if ( !$fp )
{
eZDebug::writeError( 'Could not read contents of ' . $filename, 'eZFile::getContents()' );
return false;
}

return fread( $fp, filesize( $filename ) );
}
}

I'm assuming from that that there is a renaming there somewhere, but I can't quite figure out how to work it with the code that I've currently got:
if(file_exists(_BASEDIR."messages/tinymce.txt")) $tinymcesettings = file_get_contents(_BASEDIR."messages/tinymce.txt");


Is there any possible way to get that to work with the code I'm using? Or is there an easier way (other than upgrading my PHP)?

kehers
08-04-2006, 03:57 PM
Have u confirmed the file path actually exist? this might be a good way to chek that

echo realpath(_BASEDIR."messages/tinymce.txt");
//returns false on falure
//but prints out path on success

On theother hand, there are couple of other file streaming functions u can use like fopen() + fread, fget and file().....
if for example you will be using the snippet u sent, u can use it this way:

function getContents( $filename )
{
if ( function_exists( 'file_get_contents' ) )
{
return file_get_contents( $filename );
}
else
{
$fp = fopen( $filename, 'r' );
if ( !$fp )
{
echo "Could not read contents of $filename";
return false;
}

return fread( $fp, filesize( $filename ) );
}
}
//then call the function like this:
echo getContents("path/to/myfile.ext");

Hope that helps.

NancyJ
08-04-2006, 04:16 PM
I actually had to do this recently on a really old server

We were getting file data from an external url.

this is what I did


function file_get_contents($file)
{
$handle = fopen($file, "rb");
$contents = '';
while (!feof($handle)) {
$contents .= fread($handle, 8192);
}
fclose($handle);
return $contents;

}

saifai
08-06-2006, 02:13 AM
What I ended up doing is just replacing instances of file_get_contents with getContents and it worked out great.

Thank you for helping me!