How to Write Contents to a File Using PHP


In this article, we show how to write contents to an existing file that you have in a directory on a server.

Say, you have an existing file already in a directory on your server and you want to write something to that file, such as a few lines.

Well, with PHP, you can write contents to a file without having to go that file and open it up and write the content.

Since PHP is a server side language, it can access any file in a directory on your server and write contents to it without you having to directly go to that file.

Before I go over to the PHP code, look at the file below.

This is the contents of the file, sample.txt.


So you can see the contents of the file above.

Write something to this Textarea to Write to the File Shown Above

So above is an area where you can write contents. When you click the submit button, the contents that you wrote will be written to the file, sample.txt.

If you wrote something and submitted it by clicking the submit button, you should see the contents above that show the file change to include this. You will have to refresh the page though after you submit the form to see the changes to the content of the file.

So you've now written contents to a file using PHP without having to manually go to the file and write it.

So how is this done in PHP code?

This is shown below.

PHP Code

The code to write contents to a file using PHP is shown below.

Or if you just wanted to use a single line to write contents to a file, you could use the line shown below.

So the code is very straightforward and simple.

All you have to do to write contents to a file is to use the file_put_contents() function. The first parameter in this function specifies the pathway and the filename. And the second parameter specifies the string that you want to write to the file.

Since we are writing the contents of our file to the current directory (right now this article is published in the Articles directory of this website), this file named sample.txt is saved in the sample.txt file in the Articles directory. However, if we were writing to the subdirectory named 'files', for instance, inside the Articles directory, the first parameter would be 'files/sample.txt'.

The second parameter is simply the string that you want to write.

Be aware, though, that the file_put_contents() function in PHP erases everything currently in the file and overwrites it with the content that you specify. Therefore, be careful. If you don't want to erase all the contents that you have in a file, this is not the function to use. It totally erases everything and replaces just with the text that you write to it. Therefore, make sure this is what you want when using this PHP function. You can see this above when using the textarea form.

Anyways, this just shows you briefly how to write contents to a file using the PHP file_put_contents() function.

Related Resources

How to Read the Contents of a File Using PHP

How to Copy a File Using PHP

How to Rename a File Using PHP

How to Delete a File Using PHP

How to List All Files in a Directory using PHP

HTML Comment Box is loading comments...