The Current Directory
The Current Directory
PHP Directory Function Basics – Part 3
Introduction
This is part 3 of my series, PHP Directory Function Basics. In this part of the series, we look at the use of the current directory.
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.
URL and Directory Path
Note that in the previous two parts of the series, the directory path has always begun from the root directory of the hard drive (c:/). It is possible to have a path, which begins with a URL; something like http://www.awebsite.com/directory1/directory2. In this case the directories are in the server. To achieve this you will have to do some configuration at the server. Well, you may not be allowed to do any such configuration at the server. In that case a good solution is to work with the current directory: the directory that has the PHP script, like the ones we have created in the previous parts of the series. The current directory can be the home directory at the server or any sub directory at the server that has the PHP script.
When you work with the current directory, you can access any sub directory descending from the current directory. You do not need to precede the first sub directory in the string argument with a forward slash or anything else. We have examples below.
Creating a Sub Directory in Home Directory
By home directory here, I am referring to the directory of your server that corresponds to the domain (e.g. http://www.somewebsite.com). To create a sub directory in the home directory, put the PHP script that creates sub directory in the home directory.
For now, replace the content of the temp.php file in the home directory with the following.
<?php
mkdir(“directoryA”);
?>
Try the code. Use your operating system to go to the home directory of your server and you should see the directory, directoryA.
While the current directory is the home directory, let us create directoryB in directoryA. Replace the content of the above file with the following code and try.
<?php
mkdir(“directoryA/directoryB”);
?>
Use your operating system and open directoryA and you should see directoryB. As you can see, in the string argument of the mkdir() function, the first sub directory (directoryA) is not preceded by a forward slash or anything else.
Try the following code, which creates directoryC in directory directoryB, from the current directory, which for now is the home directory.
<?php
mkdir(“directoryA/directoryB/directoryC”);
?>
You should now have directoryC in directoryB.
Accessing the Current Directory Itself
While you are at the current directory, you can use the opendir(), readdir, closedir(), mkdir() rmdir() and other functions for sub directories that are descendants of the current directory. So, with these functions, if you want to access a sub directory, you type the relative path in the string argument of the function (or depending on the function, type the corresponding handle).
What about the case when you want to access the items of the current directory itself? In this case you use a single dot to represent the current directory. Hey, while in the current directory, you can also access items in the immediate parent directory (if it exists), using two dots. Let us look at examples.
Reading Content of the Current Directory
Use your text editor to create a text file with the name, file2.txt (type anything inside the file) in the directory, directoryB. Type (copy and paste) the following and save with the name cur.php in directory directoryB (file2.txt and cur.php are two independent files in the same directory).
<?php
$ dirHandle = opendir(“.”);
while (true == ($ fileOrdirName = readdir($ dirHandle)))
{
echo $ fileOrdirName; echo “<br “;
}
closedir($ dirHandle);
?>
The aim is to list the items in the current directory. The current directory now is directoryB since it has the php file. Note that in the string argument of the function, opendir(), we have just a dot. This dot identifies the current directory.
Try the code; in the address bar of my browser I had to type, http://localhost/directoryA/directoryB/cur.php . Your browser should display something like:
.
..
directoryC
cur.php
file2.txt
All the items in the current directory are listed.
Reading Content of the Immediate Parent Directory
To read content of the immediate parent directory of the current directory, use the above procedure, but instead of one dot for the string argument, use two dots. Use your text editor to create a text file with the name, file1.txt (type anything inside) in the directory, directoryA. The aim now is to list the items of the immediate parent directory. Go to the file cur.php and replace the single dot in the string argument with two dots, where the second dot follows the first, like “..” .
Try the code. Your browser should display something like:
.
..
directoryB
file1.txt
All the items of the immediate parent directory, directoryA have been displayed.
We have done enough for this part of the series. Let us stop here and continue in the next part of the series.
Chrys
To arrive at any of the parts of this series, just type the corresponding title below in the Search Box of this page and click Search (use menu if available):
PHP Directory Function Basics
Creating and Deleting Directory in PHP
The Current Directory
Conditionals and PHP Directory Functions
Scan Directory
PHP is_file and is_dir Functions
Written by Chrys