How to Create a New File Using PHP


In this article, we will show how to create a new file on your website using PHP.

This means you can create new pages on the fly, dynamically, with PHP. This can be a new text file, HTML file, PHP file, etc.

This means you don't have to manually go to a FTP and upload a new file. You can use the PHP language to create a new file.

PHP is a powerful server-side scripting language. It can do many powerful things, one of them being creating new files on a website, backend work.

Creating new files using PHP can be very important. For example, say if you want to create a new page based on some input that a user has entered. Or say you're creating a website that acts as a notepad on the cloud where users can save files. This can be accomplished with PHP>

So how do we go about creating new files with PHP?

And we create new files with PHP through the fopen() function.

This is the same PHP function that is used to open an existing file.

However, if the file does not exist, PHP will create the file. So this is how PHP can be used to create a new file.

So below we show the code.


The code to create a new file called newfile.php is shown below.

This fopen() function is very simple. It takes 2 parameters. The first parameter is the name of the file that you want to create And the second parameter is the accessibility you want to provide to this file. If we want to be able to write to this file, then we have to give it write permissions ("w"). If we simply want to read from this file, then we give it read permissions ("r").

So the following code above create a file named newfile.php and the file is completely blank, because we haven't written anything to it.

Below, though, we'll show how to write to files.

So this code above creates a new file called newfile.php and writes the text, "I love PHP", in the file. We can write text to a file using the fwrite() function.

So this demonstrates how to write text to a new file you create with PHP.

However, be careful, because rememmber the same PHP function fopen() that opens an existing file creates a new file if an existing file does not exist.

Say if you use the fopen() function to create a new file. However, the file already exists, an important file. If you use the fwrite() function, you will erase all data in that existing file, unintentionally. To avoid this, you can use an if statement to find out if the file already exists. If it does, you probably don't want to overwrite it. So you can use the following code that if the file already exists that you are trying to create, the code will inform you of this.

Now with this code, we'll only create a new file if there is no existing file with that name. Therefore, we won't run the risk of overwriting potentially important data that we have.

The last thing is the fopen() can specify not only a file name, such as what is shown above, but also a pathway.

Say if we want to create a file in a directory other than the current directory. We can specify the pathway in the fopen() function.

The example below will store the file in the Articles directory.

So the new file we create, newfile.php, will be stored in the Articles directory if we upload it to the webroot directory.

So this is how we specify relative paths to create new files in other directories other than the current directory.

So if you upload this PHP file to the root directory, it creates the file, newfile.php, in the Articles directory.

So the fopen() function takes either the filename (if creating in the directory this PHP file is uploaded to) or the pathway and the filename (if creating in another directory other than the current directory).

And this is how to create a new file using PHP.

Related Resources

How to Create a New Directory Using PHP

How to Upload Files to Your Server Using Your Own Website

How to Restrict the Size of a File Upload with PHP

How to Create Your Own File Transfer Protocol (FTP) For Your Website Using PHP

How to Create a Register and Login Page Using PHP

How to List All Files in a Directory using PHP

How to List All the Directories of a Website Using PHP

How to List All Files of the Home Directory and Its Subdirectories of a Website Using PHP

How to Read the Contents of a File Using PHP

How to Write Contents to 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

HTML Comment Box is loading comments...