PHP file_get_contents() Function

PHP file_get_contents() Function is used to read the contents of a file into a string. A file_get_contents() function is PHP built-in function.

PHP Version : PHP 4.3+, PHP 5, PHP 7, PHP 8

Syntax for file_get_contents:

file_get_contents(string $path, bool $include_path, resource $context, int $start,int  $max_length);

Parameter,

$path : Required. It is the path to the file to read.
$include_path : Optional.Set this parameter to '1' if you want to search for the file in the include_path.
$context :
Optional.A valid context resource created with stream_context_create(). If you don't need to use a custom context, you can skip this parameter by null.
$start : Optional.It is initial point in the file to start reading. Negative values count from the end of the file
$max_length : Optional.It is maximum length of data to read.The default is to read until end of file (EOF) is reached.

Return values for file_get_contents :

PHP file_get_contents() Function returns the contents of a file into a string. Also It returns false on failure.

Let's see below example to understand php file_get_contents() Function in details.

Example :

<?php
echo file_get_contents("contents.txt");
echo "<br><b>Read file by providing start point and maximum length to read data.</b><br>";
echo file_get_contents("contents.txt",0,NULL,32, 20);
?>

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

28698