Javatpoint Logo
Javatpoint Logo

file_put_contentss() Function in PHP

file_put_contentss() is another built in PHP function which is used mainly to write a string inside a file. The main task performed by the file put command is that it identifies the file in which the data will be written by the user and if the file exists it will add the data to the file but if the file doesn't exist it will automatically create a new one.

file_put_contentss function contains multiple parameter which is necessary to be added in order to run the function properly i.e developer has to assign the path of file on which that data will be recorded and the data to be recorded as parameters in the file_put_contentss function and as result the function will return the number of bytes of data that has been recorded in file if the writing of file is successful else it will return FALSE if the writing of data fails.

the file_put_contents ( ) function mainly accepts two , major parameters ( $file, $data ) this function is used to return the number of bytes of data that has been written inside the file if the process succeed else it will return false

Syntax:


S.No Parameter Description Voluntary / mandatory
1 $ file It specifies the destination or name of the file in which the developer wants to write the content Mandatory
2 $ data It specifies the content of the data that will be written inside the file that was declared earlier, the data can be of any type it can be a string data, an array or a data stream Mandatory
3 $ mode This optional parameter helps us to specify about the way we want the data to be written on the declared file.
Types of mode parameter: -
FILE_USE_INCLUDE_PATH - this mode is used to search for a particular file in a selected directory by using the filename
FILE_APPEND - this mode is used to add the data of a new file in an existing file without creating a new file or overwriting the previous data
LOCK_EX - we use this mode to lock a particular file exclusively before writing inside the file
FILE_TEXT - we use this mode to write the data in a text method.
FILE_BINARY - we use this mode to write the data in binary mode, which is also a default process.
Voluntary
4 $ context It is an optional parameter which is used to define the stream behaviour or what is the context of the file. voluntary

Errors - one of the major disadvantage of using this function is that it fails to write inside the file if the location of directory or directory name is invalid and sometimes file_put_contents () function returns a non-Boolean character instead of false which is also equivalent to Boolean false.

Program:

Output:

69

Here in this program we have declared file_put_contents() and inside the $file parameter we have provided the location of file hello_world from and in the $data parameter we have provided the data to write inside the file

Program

Output:

content of two files appended successfully

Here in this program we have declared $file variable to add the location of the file append.txt and we have declared another variable $apnd to add the data inside the file, now we have used the file_put_contents() function with $file and $apnd as parameters we have append our file with data







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