Current Location: Home> Function Categories> file_put_contents

file_put_contents

Write data to a file
Name:file_put_contents
Category:File system
Programming Language:php
One-line Description:Writes a string to a file.

Definition and usage

file_put_contents() function writes a string into a file.

The same is true for calling fopen() , fwrite() and fclose() functions in turn.

Example

 <?php
echo file_put_contents ( "test.txt" , "Hello World. Testing!" ) ;
?>

Output:

 26

grammar

 file_put_contents ( file , data , mode , context )
parameter describe
file Required. Specifies the file to be written into the data. If the file does not exist, a new file is created.
data Optional. Specifies the data to be written to the file. Can be a string, array, or data stream.
mode

Optional. Specifies how to open/write files. Possible values:

  • FILE_USE_INCLUDE_PATH
  • FILE_APPEND
  • LOCK_EX
context

Optional. Specifies the environment for file handles.

context is a set of options that can modify the behavior of a stream. If null is used, it will be ignored.

illustrate

The parameter data can be an array (but not a multidimensional array).

Since PHP 5.1.0, the data parameter can also be specified as a stream resource. The cached data saved in the stream will be written to the specified file. This usage is similar to using stream_copy_to_stream() function.

Support for context parameters is added by PHP 5.0.0.

Return value

This function returns the number of bytes written to the data in the file.

Similar Functions
Popular Articles