
Some days before we have seen set of basic PHP file functions to perform file open, read, write or append operations. Similarly, PHP includes set of directory functions to deal with the operations, like, listing directory contents, and create/ open/ delete specified directory and etc. These basic functions are listed below.
In this article, we have to discuss each of the above basic directory functions in PHP with their corresponding usage of these functions, possible parameter to be passed if any, with suitable examples.
For creating a new directory using PHP programming script, mkdir() function used, and, the usage of this function is as follows.
mkdir($directory_path,$mode,$recursive_flag,$context);
This function accepts four arguments as specified. Among them, the first argument is mandatory, whereas, the remaining set of arguments are optional.
This function will return boolean data, that is, true on successful execution, false otherwise.
For listing the contents of a directory, we require two of the above-listed PHP directory functions, these are, opendir() and readdir(). There are two steps in directory listing using PHP program.
As its name, opendir() function is used to perform this step. And, it has two arguments, one is compulsory for specifying the path of the directory, and the other is optional, expecting stream context if any. The syntax will be,
opendir($directory_path,$context);
Unlike PHP mkdir() returning boolean value, this function will return resource data as like as fopen(), mysql_query() and etc. After receiving the resource identifier return by this function, then only we can progress with the subsequent steps to read, rewind or to close required directory with the reference of this resource id.
Otherwise, PHP error will occur for indicating the user, that the resource id is not valid.
For performing this step, we need to call readdir() function recursively until the directory handle reaches the end of the directory. For that, we need to specify the resource-id returned while invoking opendir(), indicated as directory handle.
PHP readdir() will return string data on each iteration of the loop, and this string will be the name of each item stored in the directory with its corresponding extension. For example,
$directory_handle = opendir($directory_path); while($directory_item = readdir($directory_handle)) { echo $directory_item . "<br>"; }
And, thereby executing the above code sample, we can list the content of a directory as expected.
Once the directory link is opened to perform set of dependent operations like reading directory content, we need to close this link after completing the related functionalities required. For example,
$directory_handle = opendir($directory_path); ... ... closedir($directory_handle);
We have seen in the previous article about how to delete a file from a directory using PHP unlink(). Similarly, for removing the entire directory, PHP provides a function named as rmdir() which accepts the same set of arguments, as like as mkdir(). These are, the $directory_path and $context(Optional) as stated below.
rmdir($directory_path,$mode,$recursive_flag,$context);
But, this function will remove the directory, if and only if it is empty. For removing the non-empty directory, we need to create a user define a function that recursively calls unlink() function to remove each file stored in the directory to be deleted.
The following PHP program deals with the set of directory functions, that is, for creating a new directory, open and read for listing directory content and closing directory as follows.
<?php mkdir("php_directory_functions_manual",0777); /*Creating files into php_directory_functions_manual*/ $file_pointer1 = fopen("php_directory_functions_manual/mkdir.txt","x"); $file_pointer2 = fopen("php_directory_functions_manual/rmdir.txt","x"); fclose($file_pointer1); fclose($file_pointer2); $directory_handle = opendir("php_directory_functions_manual"); while($directory_item = readdir($directory_handle)) { echo $directory_item . "<br>"; } closedir($directory_handle); ?>
This program will return the list of created files by using fopen() function as shown below.
. .. mkdir.txt rmdir.txt
And then, let us have an another example, for looking into how a directory can be removed.
<?php $directory_handle = opendir("php_directory_functions_manual"); while($directory_item = readdir($directory_handle)) { @unlink("php_directory_functions_manual/".$directory_item); } closedir($directory_handle); rmdir("php_directory_functions_manual"); ?>
On iterating with a loop for reading each item stored into the directory, the unlink() function is invoked to wipeout the directory before attempting to delete it. And then, rmdir() is used by referring with the name of the directory, to remove it.
Note: