PHP Manual
Working with files


11. 09. 2019

Obsah článku

The **file_get_contents** function is used to read a file and put its contents into a variable. This function is similar to the include function, but unlike include, it can retrieve remote files on the Internet and transfer their contents via variables.


Either function can be used to load a local file from disk:

$news = file_get_contents('news.html');
echo 'Current news:<br>' . $news;

Or from a remote URL:

$page = file_get_contents('');
echo $page;

When retrieving a URL, we can download any address and get its contents as a string into a variable. In the case of HTML, this is the source code.

The page is rendered incorrectly

This is because the HTML code is passed exactly as it is placed on the URL.

If the path to the image is for example <img src="kocka.png">, then this file may not exist in the context of our server, so we need to correct the path to for example: <img src="">.

Jan Barášek   Více o autorovi

Autor článku pracuje jako seniorní vývojář a software architekt v Praze. Navrhuje a spravuje velké webové aplikace, které znáte a používáte. Od roku 2009 nabral bohaté zkušenosti, které tímto webem předává dál.

Rád vám pomůžu:

Související články

All systems normal.