How to read the contents of a file into a string in PHP

- by

file_get_contents() can help us here. It reads a file and stores it in a string. Can be used with local files, as well as online content:

$myLocalFile = file_get_contents('myfile.html');
$myOnlineFile = file_get_contents('http://wpguru.co.uk');

echo myOnline File; // would display the actual website

This is different from the file() function which reads the contents of a file into an array.



If you enjoy my content, please consider supporting me on Ko-fi. In return you can browse this whole site without any pesky ads! More details here.

Leave a Comment!

This site uses Akismet to reduce spam. Learn how your comment data is processed.