Managing Folder with PHP

May 07, 2008

PHP programming allows directory management where you can list contents of the folder and create or delete a folder. The PHP function that can be used are as below:

  1. dir() : return a directory class which contains information about the folder specified
  2. mkdir() : creates a new directory
  3. rmdir() : deletes a directory

dir() function

This function accept one parameter which will be the folder path which you wish to check on the contents or folder propeties. This function returns a class which do have some function in it that is :

  1. rewinddir() : resets the folder content pointer to the beginning
  2. read() : read the folder contents one by one upon execution
  3. close() : close the folder object

Sample script are shown below:

$mydir = dir(”/this/is/the/folder/path”);
echo “Path: ” . $d->path . “<br>”;
while (false !== ($entry = $d->read()))�
{
     echo $entry.”<br>”;
}
$d->close();
echo “<br>”;

mkdir() function

This is used to create a folder by using the name/path specified. This function returns boolean values where TRUE is successfully created and FALSE is otherwise.

The sample script are as below:

mkdir(”newfolder”);
mkdir(”/this/is/newfolderpath”);

If you are using path, you will need to make sure the new folder parent directory exists. The function will not create the folder if the specified parent folder is not valid.

rmdir() function

This is used to delete a folder by using the name/path specified. This function returns boolean values where TRUE is successfully deleted and FALSE is otherwise.

The sample script are as below:

rmdir(”newfolder”);
rmdir(”/this/is/newfolderpath”);

You may download the sample script here.

Leave a Reply