Managing Folder with PHP
May 07, 2008PHP 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:
- dir() : return a directory class which contains information about the folder specified
- mkdir() : creates a new directory
- 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 :
- rewinddir() : resets the folder content pointer to the beginning
- read() : read the folder contents one by one upon execution
- 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.