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 returns 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.
PHP file_get_contents() Parameters
Now, let us have a look into the details on PHP file_get_contents() parameters.
- $file_name – name or path of the file from which the file source is required.
- $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.
- $context – If we want to send some resource data to get file contents, we can use this parameter.
- $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.
- $limit – This offset is used to set end limit while getting file source.
- 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 take 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.
Now, see the following PHP program for reading this source as a string output to be displayed with browsers.
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 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 start and end limit for PHP file_get_Contents function.
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.
- When we pass non-existing file by specifying a 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.