Javatpoint Logo
Javatpoint Logo

PHP file_get_contents() Function

In this article, we will perceive how to add the whole document to a string utilizing the file_get_contents() function, alongside figuring out their execution through the model.

The file_get_contents() function in PHP is a pre-recorded function that is utilized to add a report to a string. The function utilizes memory planning procedures that are upheld by the server and hence improve the exhibition, making it a favored approach to perusing the items in a record. The way the document is to be perused is sent as a boundary to the function, and it returns the information read on progress and FALSE on disappointment.

Syntax:

before taking a serious dive into the file_get_contents () function, first, we have to understand how to add context to a string, for that, we have another built-in PHP function called file_put_contents()

file_put_contents()

file_put_contents() is one more implicit PHP function that is utilized for the most part to compose a string inside a document. The principal task performed by the file put commands that it distinguishes the record where the information will be composed by the purposes, and assuming the document exists, it will add the information to the document; however, on the off chance that the record doesn't exist, it will consequently make another one.

File_put_contents function includes various boundary, which is important to be included in the request to run the function appropriately, i.e., the designer needs to allocate the way of the document on which that information will be recorded and the information to be kept as boundaries in the file_put_contents function, and as a result, the function will return the number of bytes of information that has been kept in the record if the writing of record is effective else it will return FALSE assuming the composition of information falls flat.

The file_put_contents ( ) function acknowledges two , significant boundaries ( $file, $data ) this function is utilized to return the quantity of bytes of information that has been composed inside the record if the cycle succed else it will get back false

Syntax

Parameters: The file_get_contents() function in PHP accepts one mandatory parameter and four optional parameters.

S.No Parameter Description Voluntary / mandatory
1 $ path This parameter is used by developers to define the destination or the location of the file through which they want to read the content. It can be the name of the file with the extension or the complete path. Mandatory
2 $ include _ path A voluntary boundary looks for a record in the document in the incorporated way (in php.ini) Voluntary
3 $ context It is a non-mandatory parameter that the file gets contents to hold, which is mostly utilized to determine the stream behavior or what is the context of the file. voluntary
4 $ start It is another optional parameter that is used by the developer to rectify the starting point inside a file from where we want to begin reading Voluntary
5 $ max _ length It's another optional parameter that is used by the developer to rectify the number of bytes inside a file from to be read voluntary

Errors:

  • If you have any desire to open a record with unique characters, for example, spaces, it should be encoded first utilizing urlencode().
  • The file_get_contents() work returns Boolean FALSE. However, it may likewise return a non-Boolean worth which assesses to FALSE.
  • An E_WARNING level blunder is produced in the event that filename can't be found, max length is under nothing, or on the other hand, assuming looking for the predetermined offset in the stream comes up short.

Program 1:

Output:

PHP file_get_contents() Function

Here in this program, first, we have defined our file_put_contents() function to add the content inside a particular file to perform the operation we have placed inside the $file parameter. We have provided the location of the file hello_world.txt from, and in the $data parameter, we have added the data that we have to write inside our file. And later, we used the file_get_contents() function with $path as the name of the file that we just wrote and used the echo command to receive the output.

Program: 2

Output:

PHP file_get_contents() Function

Here in this program, first, we have characterized our file_put_contents() function to add the substance inside a specific record append.txt, and we have proclaimed another variable $appended to add the information inside the record, to play out the activity we have set inside the $file we have given the area of document and $appended as parameters we have to add our record with information. What's more, later, we utilized the file_get_contents() work with $path as the name of the record that we recently composed. likewise, we have involved 0 at $start so we can peruse all along and utilized $max_length by 36 so that the program can peruse 36bytes and utilize the echo to get the result







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA