(Last Updated On: July 21, 2019)

Today, I’m going to talk about the important topic in PHP file system, file put content.

Every single detail I will try to cover on this post and you will get some bonus tips on file put content.

Let’s begin…
File Put Content has used on PHP for Write data/text to a file. File Put Content is a default function in PHP, file_put_contents(), it’s comes with four parameters.

The Syntax is: file_put_contents(file,data,mode,context);


file_put_contents("example.txt","Hello World ! \nThe Data/Text Which one did you want to add on the file.");

The Output:

If you run this example, it will create a file to name, example.txt on where form you have run the file, if already have a file example.txt, then it rewrites the file and put the data on it. And “\n” means a new line/linebacker.

File Put Content Parameters:

File and data it’s a required parameter, you must have to declare this if you are using File Put Content file and mode & context both are optional.


If you want to rewrite any existing file, add the file path with file name and extension, Same as if the fine does not exist on the path, it will create a new file.


Data means, which one did you want to add on the file, it can be string, data stream or array.


it optional, it declares the way how to open/write data on the file, It’s work on three different fixed values:

You can use one at a time or both of them at a time to use many you have to use “|”.
if you don’t use any mode by default every time on rum, it will overwrite the file and add the data form began.

FILE_APPEND will append the data form bottom of the file instead of to overwriting it, So your old data will exist and new data will be added form end of the old data.
And LOCK_EX have used to prevent multiple users writing at the same time.
And FILE_USE_INCLUDE_PATH have use for search files


It’s also optional, has used on the fileHandle to Specifies the context, you can also modify the behavior.

file_put_contents("actors.txt","\nLeonardo DiCaprio \nTom Hanks \nBruce Lee", FILE_APPEND | LOCK_EX);

Or in Different way:

$file = 'actors.txt';
$actors = "\nLeonardo DiCaprio \nTom Hanks \nBruce Lee";
file_put_contents($file, $actors, FILE_APPEND | LOCK_EX);

“To get live examples, you can watch the video”

Bonus concept with file put content:

Let’s see some interesting thing with file put content & file get content to use both functions. File put content will get the create the file and file get content will get data to form any file link.

file_put_contents("", file_get_contents(""));

You need wordpress file on your server, So you have to downalod the file and then upload.
You can simply add the WordPress files to use the example code.
Not only WordPress also you can transfer any zip file from any server, Just flow the example and change the file get the content link and file name, You can also add any type of name.
It’s cool, you can transfer a large amount of file in a few seconds.

If you have any questions, you can comment, I will try to give solutions…

Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment