How to Create a Text File With PHP

PHP

You can easily create text files from the server side with the help of a scripting language such as PHP. In this tutorial, I will show you how to create text files with PHP by providing some simple examples.

PHP is a highly capable web programming language that allows you to perform a wide variety of tasks while developing dynamic and database driven websites and web applications. One of the highly useful things you can do with PHP is to work with files and folders on your server, e.g. your website files or files within your web application. For example, you can create files and folders, you can edit them, delete them or make them available for download.

As the simplest of the file types, you can also create text files (.txt) with PHP. You can use scripts to automatically create and store text files on your server, or you may provide your users with mechanisms to create files, e.g. a "Create File" button on your website or application.

You can put any type of text into the text file as you wish. For example, you can get some data from your database and use it as the content of your file. You can get some information input from the user and put it into the text file. Or, you can read an external file on another server and save some or all of its content to your text file. You can even create HTML, CSS or JS files using the correct extension for each file type, since they are all basically text files.

We covered creating text files with JavaScript in another tutorial, which demonstrated the creation of text files from the client (web browser) side. We will now see how creating text files with PHP works.

Also Check:
- PHP MySQL Database Connection Class

Create Text Files with PHP

We will divide the file creation process into three steps, which are creating/opening the file, writing content into the file and closing it. All these three tasks are carried out with different PHP functions as you will see below. At the end of the tutorial, we will also mention another function that handles all these three tasks alone.

While following the tutorial, don't forget to test your PHP script to see how each step works. If you stumble upon any issues and receive errors, make sure that file creation functions are enabled on your web server and also check that the directory you are working on has appropriate file read/write permissions.

Now, create an empty PHP file (e.g. create-file.php) and continue with the steps below.

STEP 1: Create File with PHP

In order to create a file in PHP, we use the fopen() function. This function serves for two purposes: 1) Creates a file if it doesn't exist and 2) Opens a file if it already exists.

Below is an example with the fopen() function; simply insert the following code into your PHP file and run a test to see how the file is created:

<?php
$file_name = "file.txt";
$file = fopen($file_name, "w");
?>

As you see in the above code, the fopen() function takes two parameters. The first parameter ($file_name) is the name of the file, which you can assign as a variable like I did if you want or simply use the file name between quotes. The second parameter (w) is the file open mode. "w" is used for creating a file and for writing to a file, while "r" is used for reading from a file and "a" is used for appending content to a file.

Note that, if your file is going to be stored in a directory other than where the current PHP script is in, you need to include the correct path in the file name as follows:

<?php
$file_name = "files/file.txt";
$file = fopen($file_name, "w");
?>

In the above example, file.txt will be created in the files folder, which is in the same directory with the PHP file creation script.

STEP 2: Write to File with PHP

Once the file is created, our next step is to write something onto the file. We use fwrite() function for that.

<?php
$file_name = "file.txt";
$file_content = "Enter the file content here.";

$file = fopen($file_name, "w");
fwrite($file, $file_content);
?>

The fwrite() function also takes two parameters: The first parameter is the file handle we created above ($file) and the second parameter is the text content of the file ($file_content).

The file content can be plain text, HTML, CSS, JS or even PHP code. Just pay attention to the correct use of double quotes while preparing the content of the file manually. One other option is to read another file and copy its content to the new file.

STEP 3: Close File with PHP

Once you are done with inserting content to your file, it is recommended to close it so that it won't continue to use any system resources. We use fclose() function to close files in PHP.

<?php
$file_name = "file.txt";
$file_content = "Enter the file content here.";

$file = fopen($file_name, "w");
fwrite($file, $file_content);
fclose($file);
?>

fclose() function takes one parameter, which is the file handle.

Create File with PHP file_put_contents() Function

You can do all these three steps using one single function, which is file_put_contents(). The following code demonstrates the use of this function:

<?php
$file_name = "file.txt";
$file_content = "Enter the file content here.";

file_put_contents($file_name, $file_content);
?>

The file_put_contents() function takes two parameters, the file name and the file content. As you can see this is a slightly simpler way of creating text files in PHP. Though, both methods have their own specific use cases for sure.

After creating the text file, what you do with it is up to you. You can either store it on your server or prepare it for download, or simply delete with another function/script, after it serves its purpose. You can check our tutorial about how to download a file with PHP to learn how file downloads work. You can even create a ZIP archive if you prefer to deliver your file or files as a ZIP archive.

f t g+ in