• PHP Video Tutorials

PHP - Function file_get_contents()



The file_get_contents() can read a file into a string. This function is the preferred way to read the contents of a file into a string because it can use memory mapping techniques if this is supported by the server to enhance performance.

Syntax

string file_get_contents ( string $filename [, bool $use_include_path = FALSE [, resource $context [, int $offset = 0 [, int $maxlen ]]]] )

This function is similar to file() function except that file_get_contents() function returns the file in a string starting at specified offset up to maxlen bytes.

Example-1

<?php
   $file = file_get_contents("/PhpProject/sample.txt", true);
   echo $file;
?>

Output

tutorialspoint
tutorix

Example-2

<?php
   $section = file_get_contents("/PhpProject/sample.txt", NULL, NULL, 4, 10);
   var_dump($section);
?>

Output

string(10) "rialspoint"
php_function_reference.htm
Advertisements