PHP file_put_contents() ফাংশন

Definition and Usage

The file_put_contents() function writes a string to a file.

Is the same as calling fopen(), fwrite(), and fclose() sequentially.

Syntax

file_put_contents(file,data,mode,context)
Parameter Description
file Required. Specifies the file to write the data to. If the file does not exist, a new file will be created.
data Optional. Specifies the data to be written to the file. It can be a string, an array, or a data stream.
mode

Optional. Specifies how to open/write to the file. Possible values:

  • FILE_USE_INCLUDE_PATH
  • FILE_APPEND
  • LOCK_EX
context

Optional. Specifies the environment for the file handle.

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

Description

Parameter data can be an array (but cannot be a multidimensional array).

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

for context Parameter support was added in PHP 5.0.0.

Return Value

This function will return the number of bytes written into the file.

Tips and Comments

Tip:Using FILE_APPEND can prevent the deletion of existing content in the file.

Comment:This function can be safely used for binary objects.

Example

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

Output:

26