PHP Manual
/
Working with files

File_get_contents

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.

Sample

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('https://www.google.com');
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="https://server.cz/kocka.png">.

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

1.
5.
Status:
All systems normal.
2024