PHP Directory Function Basics
PHP Directory Function Basics – Part 1
Forward: You can use PHP to access a directory, to know the content (files and sub directories) of a directory, to create a directory and to delete a directory. I show you how to do that in this article series.
By: Chrysanthus Date Published: 2 Jul 2013
You need basic knowledge in PHP and HTML in order to understand this series; you also need basic knowledge in handling directories and files using the operating system. The way programming is done today has had some additions to what was common 12 years back. PHP is not more than 12 years old. PHP has functions that handle directories the way it was common with other programs more than 12 years back and PHP also has functions to handle directories in new ways. The PHP function descriptions I give you in this part of the series are similar to function descriptions in other computer languages 12 years back. In part 2 of the series onwards, I will describe PHP functions that have new operational features.
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 firstname.lastname@example.org. That is, contact me for the slightest problem you have about what you are reading.
Requirements to test the Code Samples
In order to test the code samples in this series, you need web server and a browser. Most computers today have browsers, so you probably already have a browser. If you do not have access to a server, you can download a personal web server free from the Internet; just search the Internet to obtain one; look for one that works with PHP. You can also use the famous Apache server in your personal computer. In fact, that is what I use in my personal computer for this article series. Of course, you also need to have installed PHP in your server computer (or personal computer). PHP can be obtained free from the Internet.
In this part of the series, we look at functions that are used in PHP today, and that were used in other languages more than 12 years back. Even though I say that these functions were used in other languages, more than 12 years back, they were not used in exactly the same way; so do not go to some other language and type exactly what you see here.
Opening and Closing a Directory
In many languages, today and 12 years back, in order to access a directory, you have to do what they call “Open the Directory”. You then access the directory and do what you want to do with the content. After all that you do what they call, “Close the Directory”.
In PHP, opening a directory means, getting information about the directory from disk into a memory location. This information excludes contents of the files and it excludes the contents of the sub directories in the directory of interest. In PHP, the reference to this memory location is an example of what is called a resource. A resource is a reference you cannot use as you use other references. Let us not worry much about that for this series. In PHP, you can call the reference to directory information in memory, a Directory Handle.
If you do not understand PHP reference, you can read my article titled, PHP Reference. To access this article, just type the title with my name, Chrys in the Search box of this page and click Search. If on this page, you have the Google Search box, use it.
In PHP, closing the directory means freeing the memory location in memory.
In this part of the series, we shall see how to open a directory and how to close it. You use the PHP function, opendir() to open a directory and the PHP function, closedir() to close the directory. While a directory is opened, you can use two PHP functions, which are readdir() and rewinddir() on the directory information in memory. The readdir() function is used to read the directory content from the information in memory (not from disk). I will not talk about the rewinddir() function in this series.
When you use the opendir(), you can then use the readdir() function, after that you have to use the closedir() function to close the directory. For the rest of this part of the series, we shall look at these three functions.
In your root directory (c:) of your computer, use your operating system to create a directory with the name, directory1. In this directory created, create another directory with the name, directory2. In directory2, create a new directory with the name directory3. In directory3 create two sub directories with names, dirA and dirB. Also in this directory3, use your text editor to create two files; one with name, fileA and another with name, fileB. So in directory3, you have 2 sub directories and 2 files. We shall use this setup for the code samples of this part of the series.
The opendir() Function
In simple terms, the syntax of the opendir() function is:
resource opendir ( string $path)
The argument is a directory path that ends with the directory name of interest; e.g. c:/directory1/directory2/directory3. Here, directory3 is the directory of interest. The return value of the function is a reference, precisely a resource (called a handle) for the directory information in memory.
That is basically what it is to open a directory; we shall see a code example shortly.
The readdir() Function
The information of the directory in memory has the names of files and sub-directories of the actual directory in the disk. The readdir() function reads this names from the information in memory. The syntax of this function is:
string readdir ([ resource $dir_handle ] )
The function takes the resource of the opened directory as argument and returns a string. It returns the file names and/or directory names one by one as follows. As soon as the directory is opened, the directory handle “points” to the first name in the list of the directory information in memory. When the readdir() function is called, the first item is read and then the handle then points to the next item. Calling the function again would read the next item. To read all the items (file names and directory names), you need to call the readdir() function again and again. The name of the item read is returned as a string.
The closedir() Function
When you are through with the usage of the directory information in memory you have to close the directory, with the closedir() function. The syntax to close the directory is:
void closedir ([ resource $dir_handle ] )
It takes the resource of the opened directory as argument and returns nothing.
In this code example, we open the directory directory3 that you created and then read the items in the directory. I use Windows XP SP2, Apache and PHP. In my system there are actually six items (not four as you might think). Two of them are not created by me. The first item is a single dot and the other is a double dot, both not created by me. Read the following code. Save it with the name, temp.php in your home (domain) directory of your server. With Apache server, the home directory is called, htdocs.
$dirHandle = opendir("c:/directory1/directory2/directory3");
$firstItem = readdir($dirHandle); echo $firstItem; echo "<br />";
$secondItem = readdir($dirHandle); echo $secondItem; echo "<br />";
$thirdItem = readdir($dirHandle); echo $thirdItem; echo "<br />";
$fourthItem = readdir($dirHandle); echo $fourthItem; echo "<br />";
$fifthItem = readdir($dirHandle); echo $fifthItem; echo "<br />";
$sixthItem = readdir($dirHandle); echo $sixthItem; echo "<br />";
Use your browser to execute the file, temp.php. To do that you might have to type something like, http://localhost/direct1/temp.php at the address bar. My server is in my personal computer, so I have http://localhost as my domain.
The first line opens the directory, directory3 of the path, c:/directory1/directory2/directory3. Six lines follow this first line in the code. We created four items in directory3. In your system, there are probably six items. The first is a dot. The second is a double dot. In my system, the next two are the two files and the last two are the two directories.
The six lines that follow the first in the above code, each has two statements. The first of each statement reads an item name from directory3; the second echoes the name read to the browser.
The output for my browser, is,
You may not know how many files and sub directories are in the directory of interest; however, you want to see all the names of the different files and sub directories. Also, the number of files and directories may be many. In this case, you need a while loop to get all the names of the files and directories. The following code solves the problem for the above example (directory setup):
$dirHandle = opendir("c:/directory1/directory2/directory3");
while (true == ($fileOrdirName = readdir($dirHandle)))
echo $fileOrdirName; echo "<br />";
The above code is used when the list of files and sub directories is long. In fact, it is what people use all the time; people hardly use the previous code. Let us look at the while condition of this code. After each while iteration the handle points to the next item in the directory. If there is a file or sub directory at a particular iteration, the expression, ($fileOrdirName = readdir($dirHandle)) would return true. The while condition is:
true == ($fileOrdirName = readdir($dirHandle))
If the expression returns true, the comparison will be made with “true” in the condition to give an overall true and the while block will be executed. If the expression returns false, the comparison will be made to give an overall false, and the while block will not be executed. Now, the whole while condition looks ambiguous, however, that is what we have to do, as required by the PHP specification.
The variable, $fileOrdirName is not declared independently, outside the while condition. It is declared in the while condition (in the expression), and will be seen in the while block.
Note that each time the while condition is executed, the expression is executed, and the name of a file or sub directory is read, by the expression.
Well, we have done a lot for this part of the series. You are now able to read the list of files and sub directories from a directory. The question, you probably have in mind now is, “how do you know whether the name read is that of a file or a sub directory?” We shall know the solution to this later; in one of the next parts of the series. For now, let us take a break.
Related LinksBasics of PHP
PHP Directory Function Basics
Understanding PHP Reference
PHP Function Arguments
Understanding Variable Scope in PHP
Object Oriented Programming in PHP
PHP Data Types Simplified
Exception Handling in PHP
PHP Regular Expressions
Sending Email with PHP
Date and Time in PHP
PHP String Functions for Website Design
PHP Variable Scope
Advanced Features of PHP Function
Array in PHP
PHP Two Dimensional Arrays
Understanding Object Oriented Programming in PHP
Advanced PHP Regular Expressions
Some features of PHP Entities
PHP Web Application
More Related LinksMajor in Website Design
Web Development Course