Backend Development 5 min read

PHP file_put_contents() Function: Syntax, Parameters, and Usage Examples

This article explains the PHP file_put_contents() function, detailing its syntax, parameters (filename, data, mode, context), and provides multiple code examples demonstrating writing strings, appending data, handling arrays as JSON, and setting file permissions, along with usage tips.

php中文网 Courses
php中文网 Courses
php中文网 Courses
PHP file_put_contents() Function: Syntax, Parameters, and Usage Examples

The PHP function file_put_contents() is used to write data to a file. Its basic usage is to write the specified data to the specified file. The function can accept multiple parameters, including filename, data, mode, and context.

Function Syntax:

<code>file_put_contents(filename, data, mode, context)</code>

Parameter Description:

filename : required. The name of the file to write to.

data : required. The data to write; can be a string, array, or object.

mode : optional. Write mode, default 0 (overwrite). Use FILE_APPEND to append data.

context : optional. Stream context for additional options such as file permissions.

Function Usage Examples:

1. Write a string to a file:

<code>$data = "Hello, World!";
file_put_contents("file.txt", $data);
</code>

The above code writes the string "Hello, World!" to file.txt . If the file does not exist, it is created; if it exists, its contents are overwritten.

2. Append a string to the end of a file:

<code>$data = "Hello, World!";
file_put_contents("file.txt", $data, FILE_APPEND);
</code>

This code appends the string "Hello, World!" to the end of file.txt . If the file does not exist, it is created; otherwise the new data is added after the existing content.

3. Convert an array to JSON and write to a file:

<code>$data = array("name" => "John", "age" => 30);
$jsonData = json_encode($data);
file_put_contents("file.json", $jsonData);
</code>

The array is encoded to a JSON string and written to file.json .

4. Write to a file while setting file permissions:

<code>$data = "Hello, World!";
file_put_contents("file.txt", $data, 0, stream_context_create(array('chmod' => 0644)));
</code>

This writes the string to file.txt and sets the file's permissions to 0644 .

The file_put_contents() function also supports writing via URLs and using additional stream context options; choose the appropriate usage based on your needs.

When using file_put_contents() , ensure the target file and its directory have write permissions; otherwise the operation may fail, and proper error handling should be implemented.

PHP Learning Recommendations:

Vue3+Laravel8+Uniapp Beginner to Practical Development Tutorial

Vue3+TP6+API Social E‑commerce System Development Course

Swoole From Beginner to Master Recommended Course

Workerman+TP6 Real‑time Chat System Limited‑time Offer

Backend DevelopmentFile I/Ocode examplesfile_put_contents
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.