PHP Function of the Day (2011-9-19): file_get_contents

I’m skipping weekends with this btw…

[fphp]file_get_contents[/fphp] is the cleanest way to fetch a file in php. The offset and maxlength parameters will normally not see use unless the file is large enough to exhaust PHP’s memory allowance.


$file = file_get_contents($path);

Only thing I have to say here is to note the return type on this function - is String, not a file pointer, and altering the output does not change the file (though you could use file_put_contents to do so; it’s slightly wasteful, as it will open/close the file twice.)

Good to note that. Yeah, this function and it’s cousin are for “quick and dirty” file manipulations. More intensive operations are better handled with the older commands or a file iterator.