Is there any way to assign the contents of a text/html file with some php to a string variable? I'd like to be able to do something like:
$string = include("file.inc");
This doesn't seem to work. Does anyone have any suggestions.
there is a way but it nots that easy. try this.
<?php $filename="file.in"; $fpr=fopen($filename, "r") or die(); $string=fread($fpr, filesize($filename)); fclose($fpr); ?>
$string will then have the entire contents of the file file.inc.
Mark.
Thanks Mark.
Actually, what I ended up doing was:
$string = join("", file("file.inc"));
Mark's way is certainly going to perform better, if all you want is the contents into a single string variable. Your way (though fewer lines of code) creates an array from the file and then immediately tears it down again. Lots of memory allocation/deallocation going on...