PHP file_get_contents()

by Vincy. Last modified on March 5th, 2024.

In PHP, the file_get_contents() function is one of the file handling functions available in this scripting language. This function reads the entire file source or part of it and returns it as PHP string data.

This is the 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.

<?php
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.
  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 the end limit while getting the file source.

Note:

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

PHP file_get_contents() Example

Let us have two PHP programs for example 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;
?>

The first echo statement will display the output of the HTML file by displaying the form input field with a submit button. And, we are using PHP string replacements to replace the HTML tag symbol(angled bracket) with its appropriate HTML entities.

And, we have also replaced 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 the 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 the start and end limit for the file_get_contents() function.

Cautions

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

Download PHP file_get_contents() Source Code

Vincy
Written by Vincy, a web developer with 15+ years of experience and a Masters degree in Computer Science. She specializes in building modern, lightweight websites using PHP, JavaScript, React, and related technologies. Phppot helps you in mastering web development through over a decade of publishing quality tutorials.

Comments to “PHP file_get_contents()”

  • shubham says:

    sir in crop image in php tutorial how to save croped image in folder or how to move croped image in folder

Leave a Reply

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

↑ Back to Top

Share this page