file_get_contents

(PHP 4 >= 4.3.0, PHP 5)

file_get_contents -- Reads entire file into a string

Description

string file_get_contents ( string filename [, bool use_include_path [, resource context]])

Identical to file(), except that file_get_contents() returns the file in a string. On failure, file_get_contents() will return FALSE.

file_get_contents() is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by your OS to enhance performance.

Eksempel 1. Using file_get_contents() with a URI

If you're opening a URI with special characters, such as spaces, you need to encode the URI with urlencode().

<?php
$url
= "http://foobar.com/with funny chars";
list(
$protocol, $uri) = split("//", $url);
$html = file_get_contents($proto . "//" . urlencode($uri));
?>

Bemærk: Denne funktion er binary-safe.

Vink: Du kan bruge en URL som filnavn med denne funktion, hvis fopen wrappers er slået til. Se fopen() for flere detaljer omkring hvordan man udformer filnavnet og Appendiks L for en liste over understøttede URL-protokoller.

Bemærk: Context understøttelse blev tilføjet i PHP 5.0.0.

Advarsel

Ved brug af SSL, vil Microsoft IIS overtræde protokollen, ved at lukke forbindelsen uden at sende en close_notify indikator. PHP vil reportere dette som "SSL: Fatal Protocol Error" når du når til slutningen af datastrømmen. For at undgå dette, bør du sænke din error_reporting, til ikke at inkludere advarsler. PHP 4.3.7 og højere kan opdage den fejlaktige IIS server software når du åbner en stream ved hjælp af https:// wrapperen og vil skjule advarslen for dig. Hvis du benytter fsockopen() til at lave et ssl:// socket, er du selv ansvarlig for at opdage og skjule advarslen.

See also fgets(), file(), fread(), include(), and readfile().