PHP file_get_contents()

In PHP, file_get_contents() function is one of the file handling functions available in this scripting language. This function reads entire file source or part of it and return as a PHP string data. This is most widely used function among other dedicated functions used for PHP file read. For example, file(), fread() and etc.

PHP syntax for file_get_contents()

We should use file_get_contents() in PHP scripts, with the syntax as shown in the following code block.

file_get_contents(string $file_name, bool $use_include_path = false, resource $context, int $start = -1, int $limit)

PHP file_get_contents() Parameters

Now, let us have a look into the details on PHP file_get_contents() parameters.

  1. $file_name – name or path of the file from which the file source is required.
  2. $use_include_path – This is a flag and will accept boolean values. If it is TRUE, then the value of FILE_USE_INCLUDE_PATH, PHP constant will be used while searching the file. In recent versions of PHP 5, we should specify FILE_USE_INCLUDE_PATH instead of boolean TRUE.
  3. $context – If we want to send some resource data to get file contents, we can use this parameter.
  4. $start – This parameter is used to set the starting offset of a file to read its content from some point in the middle of the file source.
  5. $limit – This offset is used to set end limit while getting file source.

php_file_get_contents

Note:

  • The $start and $limit offsets are used to get some portion of file content, instead of getting entire file source.
  • The default value of start is the actual starting point of the file content and the limit will took the length of file source by default.

PHP file_get_contents() Example

Let us have two PHP programs for example of getting entire file content and some portion of the content, respectively.

Getting Entire File Content using PHP file_get_contents()

In this PHP example, we are going to use file_get_contents() function for reading the following HTML source saved as title_form.html. We have already seen this code with PHP HTML embedding example.

<html>
<head>
<title>Importing HTML</title>
</head>
<body>
<form name="frmTitle">
<tr>
<td>Title</td>
<td>
<input type="text" name="title" />
<input type="submit" value="Submit" />
</td>
</tr>
<tr>
</form>
</body>
</html>

Now, see the following PHP program for reading this source as a string output to be displayed with browsers.

<?php
$file_name = "title_form.html";
$file_content = file_get_contents($file_name);
echo $file_content . "<br/>";

/* Printing file content including HTML tags */
$html_content = str_replace("<","<",$file_content);
$html_content = str_replace(">",">",$html_content);
$html_content = str_replace("\r\n","<br/>",$html_content);
echo $html_content;
?>

First echo statement will display the output of HTML file by displaying form input field with a submit button.And, we are using PHP string replace to replace HTML tag symbol(angled bracket) with its appropriate HTML entities. And, we have also replaces carriage return escape sequences with HTML line breaks to display file content with good readability.

Getting Limited Bytes of File Content

Now, we are going to get some portion of file content by specifying start and end limit for PHP file_get_Contents function.

<?php
$file_name = "title_form.html";
$head_content = file_get_contents($file_name,FALSE,NULL,7,48);
printHTMLContent($head_content);
$form_content = file_get_contents($file_name,FALSE,NULL,62,157);
printHTMLContent($form_content);

/* Printing file content including HTML tags */
function printHTMLContent($content) {
$content = str_replace("<","<",$content);
$content = str_replace(">",">",$content);
$content = str_replace("\r\n","<br/>",$content);
echo $content . "<br/>";
}
?>

Using this program, we can get HTML HEAD portion and FORM content by setting start and end limit for the file_get_contents() function.

Cautions

  • When we pass non-existing file by specifying file name with spelling mistakes or anything, then this function will display PHP file not found error notice to the browser.
  • On the other hand, if we set $start and $limit parameters of PHP file_get_contents() function with an out of bound values, then also PHP notice error will occur.

Download PHP file_get_contents() Source Code

This PHP tutorial was added on September 22, 2013.

«

»

↑ Back to Top