How to Create a New Directory Using PHP



PHP


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

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

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

Creating directories using PHP can be very important. For example, say if you have a website that has users. Say, users store photos and documents on the website. Many times you probably want all of the user's documents, including photos, in the user's own directory. So, for example, when a user initially signs up for your website, you may just PHP dynamically to create a directory for that user. Then when the user decides to upload photos, documents, etc., you can store all in that directory that was created. More than likely, that directory name will be the same as the username, being that the username always has to be unique.

So this could be one of the ways how PHP can be used and why would it would be used to create directories.

So how do we do this? How do we create a directory?

And we create a directory through the PHP mkdir() function.

This function is very simple. All you need to know is that it takes one parameter, which is the name of the directory that you want to create.

So below we show the code.

Code

The code to create a directory called Pocahontas is shown below.





So the following code above creates a directory called Pocahontas.

You can also specify relative directories. So if I upload PHP file above to the root directory, it creates the directory, Pocahontas, in the root directory. So this website is http://www.learningaboutelectronics.com/. So this directory will be created at http://www.learningaboutelectronics.com/Pocahontas

Now you can also specify relative pathways. So if I wanted to create a directory Pocahontas in the Articles directory, I would specify the pathway. This is shown in the code below.



Now if you upload this PHP file to the root directory, it creates the directory, Pocahontas, in the Articles directory.

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

The mkdir() function is pretty safe to use. What I mean by this is that, it will not create a new directory to a directory that already exists. So this function doesn't run the risk of overwriting an existing directory that you may have that contains important files. If you attempt to use the mkdir() function to create a directory that already exists, PHP will throw a warning error, stating the following, Warning: mkdir() [function.mkdir]: File exists. So there's one way of overwriting an important directory if a mistake or made or anything.

However, if you want to create a PHP code that shows if a directory has been successfully created or if it hasn't been, you can use the following code shown below.



So this PHP code will output to you whether the directory has been successfully created or not, based on the fact if it's already taken.

So PHP is very dynamic. It can create new directories on the fly. You don't have to do it manually. It can be important for a wide variety of reasons, when doing it manually is painful and unnecessary, like the example I gave. If you have a website that has users and they can upload files, you probably want to create a directory for each user at the time they become users or at least at the time they upload files. Then you can just store all their files in that one folder.

So the mkdir() function is a great function, and this is how you can use it to create a new directory using PHP.

Related Resources

How to Create a File 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...