Creating and Deleting Directory in PHP

PHP Directory Function Basics - Part 2

Chrys Forcha
Introduction
This is part 2 of my series, PHP Directory Function Basics. In this part of the series, you learn how to create and delete sub directories in PHP.

Note: If you cannot see the code or if you think anything is missing in this article (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Make Directory
From the point of view of PHP inventors, creating a directory means making a directory, while deleting a directory means removing a directory. To make a directory (sub directory), you use the function,

mkdir()

To remove a directory (sub directory), you use the function,

rmdir()

The function names resemble their meanings.

The mkdir() Function
In simple terms the syntax of the mkdir() function is,

mkdir(string $dirname)

The argument is the path ending with the new directory name in a string, e.g. "c:/directory1/directory2/directory3/dirC", where dirC is the new directory name. Replace the content of the file temp.php with the following code and try it:

When you try the code, your browser will not display anything, since the code does not have any feedback (echo) statement. However, the directory should have been created. Use your operating system to go to the directory, c:/directory1/directory2/directory3, and you will see the new directory, dirC.

The rmdir() Function
The rmdir() function is used to remove a directory. Note; the directory must be empty before it can be removed. An empty directory might have the dot and double dot items; under such condition the directory is still empty. In simple terms the syntax for the rmdir() function is,

rmdir ( string $dirname)

The argument is the path ending with the empty directory name in a string, e.g. "c:/directory1/directory2/directory3/dirC", where dirC is the empty directory name. The following code removes the directory we created above:

Try the code (replacing the previous one). The browser will not display anything, since the code does not have any feedback (echo) statement. Use your operating system to go to the directory, c:/directory1/directory2/directory3, and you will no longer see the directory, dirC.

No Opening and Closing of Parent Directory
Note that with the mkdir() and rmdir() functions, you do not need to open and close any directory (parent).

I hope you are understanding PHP Directory Function Basics. Let us stop here and continue in the next part of the series.

Chrys

Published by Chrys Forcha

I have more than 10 years experience in computer programming, software, electronics and telecommunications. I have a First Degree in Electronics and a Master's Degree in Technical Education. As well a...  View profile

To comment, please sign in to your Yahoo! account, or sign up for a new account.