Here we will discuss how to create directories, manipulate them etc using simple php script.A complete reference for php directory manipualtion.
Let’s start with simple listing the contents of a directory. We need three functions to perform this task: opendir(), readdir() and closedir(). The opendir() function takes one parameter, which is the directory we want to read, and returns a directory handle to be used in subsequent readdir() and closedir() calls. opendir() returns False if the directory could not be opened.
The readdir() function takes one parameter, which is the handle that opendir() returned and each time we call readdir() it returns the filename of the next file in the directory. readdir() returns False if the end of the directory has been reached. Note that readdir() returns only the names of its items, rather than full paths.
The example below creates a select box that lists all the files in a directory. Copy and paste this code and save it as index.php in a directory you wish do display all files for. It automatically excludes itself from the list, and is easy to modify to make it ignore other files as well:
<?php
// open the current directory
$dhandle = opendir(‘.’);
// define an array to hold the files
$files = array();
if ($dhandle) {
// loop through all of the files
while (false !== ($fname = readdir($dhandle))) {
// if the file is not this file, and does not start with a ‘.’ or ‘..’,
// then store it for later display
if (($fname != ‘.’) && ($fname != ‘..’) &&
($fname != basename($_SERVER['PHP_SELF']))) {
// store the filename
$files[] = (is_dir( “./$fname” )) ? “(Dir) {$fname}” : $fname;
}
}
// close the directory
closedir($dhandle);
}
echo “<select name=\”file\”>\n”;
// Now loop through the files, echoing out a new select option for each one
foreach( $files as $fname )
{
echo “<option>{$fname}</option>\n”;
}
echo “</select>\n”;
?>
First, we open the directory for reading with the opendir() function and use a while statement to loop through each of its entries. We call readdir() as part of the while statement’s test expression, assigning its result to the $fname variable. We are explicitly testing whether the return value is equal to and of the same type as False since otherwise, any directory entry whose name evaluates to False will stop the loop prematurely. Within the body of the while statement, we check if the file is not this file, and does not start with a . (current directory) or .. (parent directory) and then store the file name into the $files array. We also do one more check. If a full file path leads to a directory then we add to the file name “(Dir)”.
There is another way to iterate over all files in a directory. PHP 5 has a set of objects called iterators. Iterators help eliminate problems in your code. For instance, PHP 5 provides a DirectoryIterator:
<?php
echo “<select name=\”file\”>\n”;
foreach (new DirectoryIterator(‘.’) as $file) {
// if the file is not this file, and does not start with a ‘.’ or ‘..’,
// then store it for later display
if ( (!$file->isDot()) && ($file->getFilename() != basename($_SERVER['PHP_SELF'])) ) {
echo “<option>”;
// if the element is a directory add to the file name “(Dir)”
echo ($file->isDir()) ? “(Dir) “.$file->getFilename() : $file->getFilename();
echo “</option>\n”;
}
}
echo “</select>\n”;
?>
This example produces the same results as the earlier code that uses the directory functions, but this code is shorter and safer because you cannot forget the !== = comparison.
Deleting the Directory and Its Contents
PHP has the rmdir( ) function that takes a directory name as its only parameter and will remove the specified directory from the file system, if the process running your script has the right to do so. However, the rmdir() function works only on empty directories. The example below deletes empty directory named “temporary”:
<?php
rmdir( “temporary” );
?>
If you want to delete non-empty directory, you should use the recursion. In the following example we create recursive function named deleteDir() that takes a directory name as a parameter and will go through each subdirectory, deleting files as they go. When the directory is empty, we use rmdir() to remove it.
<?php
function deleteDir($dir) {
// open the directory
$dhandle = opendir($dir);
if ($dhandle) {
// loop through it
while (false !== ($fname = readdir($dhandle))) {
// if the element is a directory, and
// does not start with a ‘.’ or ‘..’
// we call deleteDir function recursively
// passing this element as a parameter
if (is_dir( “{$dir}/{$fname}” )) {
if (($fname != ‘.’) && ($fname != ‘..’)) {
echo “<u>Deleting Files in the Directory</u>: {$dir}/{$fname} <br />”;
deleteDir(“$dir/$fname”);
}
// the element is a file, so we delete it
} else {
echo “Deleting File: {$dir}/{$fname} <br />”;
unlink(“{$dir}/{$fname}”);
}
}
closedir($dhandle);
}
// now directory is empty, so we can use
// the rmdir() function to delete it
echo “<u>Deleting Directory</u>: {$dir} <br />”;
rmdir($dir);
}
// call deleteDir function and pass to it
// as a parameter a directory name
deleteDir(“temporary”);
?>
Another way to delete non-empty directory is to use RecursiveDirectoryIterator and RecursiveIteratorIterator. The RecursiveIteratorIterator must be told to provide children (files and subdirectories) before parents with its CHILD_FIRST constant. Have a look at the code:
<?php
function deleteDir($dir) {
$iterator = new RecursiveDirectoryIterator($dir);
foreach (new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $file)
{
if ($file->isDir()) {
rmdir($file->getPathname());
} else {
unlink($file->getPathname());
}
}
rmdir($dir);
}
deleteDir(“temporary”);
?>
Note: The CHILD_FIRST constant was added with PHP 5.1
Creating new directories in PHP is accomplished using the mkdir() function, which takes two parameters. These parameters are, in order, a directory name you want to create and a permission mode for a new directory, which must be an octal number. The mode parameter is optional and has an effect only on Unix systems. Have a look at the example:
<?php
// if /path/to/my exists, this should return true
// if PHP has the right permissions
mkdir(“/path/to/directory”, 0777);
?>
By default, the mkdir() function only creates a directory if its parent exists. In PHP 5 the recursive parameter was added which should be true or false, depending on whether you want to create parent directories or not. In the following example, we pass true as a third parameter to mkdir(), so this makes the function act recursively to create any missing parent directories.
<?php
// will create /path/to/directory and
// also create /path and /path/to if needed and allowed
mkdir(“/path/to/directory”, 0777, true);
?>
Incoming search terms:
- getting the full path from an input file using jquery (4)
- extjs absolute path (2)
- object oriented php combobox onchange and text box with mysql (2)
- how to remove textbox of fileupload in php (2)
- phpmyadmin uploaddir (2)
- inurl:upload php (2)
- timer countdown in php mysql (1)
- php: iterate through files in subdirectories (1)
- yii passing variable to model (1)
- php reset ob_start (1)
- php mysql basename(getenv($_SERVER tutorial (1)
- paginated livesearch php (1)
- ie9 do you want to open or save close event (1)
- multiple set path in extjs4 (1)
- jquery how to get full path of file input (1)
- yii calendar clock (1)
- phpmyadmin inurl:database txt (1)
- size input text symfony2 (1)
- tutoriel event calendar script php (1)
- select en symfony2 ManyTomany (1)
- searcing a file from folder javascript w3school (1)
- script php upload file con scadenza (1)
- recursive directory listing php ajax (1)
- where is htaccess j2ee (1)
- while loop in fpdf (1)
- recursive directory listing in php with ajax (1)
- inurl:file/upload php (1)
- how call folder contents in to array using perl (1)
- getting the full path of image from an input file using jquery (1)
- find folder search php script (1)
- extjs messagebox is not showing onbeforeunload event of window in js (1)
- ajax php recursive directory (1)
- extjs 4 get Full Path firefox (1)
- evento al expand/collapse jqgrid (1)
- drop down menu fpdf php (1)
- how to auto calculate in textbox in php using jquery (1)
- how to calculate dynamic textbox values in jquery (1)
- How To Display BLOB Images in java and jqgrid (1)
- inurl:/upload php intitle:simple File Upload (1)
- inurl:/path/upload php (1)
You will also be interested in ,
- Send Email to Multiple Persons using php
- Simple php watermark script
- Appending string using php to a text file
- Enabling curl on XAMPP for Windows
- Check whether url or domain exists using php
- Simple birthday script using php and mysql
- Creating random number using php
- Autosuggestion / Autocomplete script using PHP and Ajax
- Output buffering using ob_start in php
- Store Data In Remote DataBase Using cUrl or Execute a HTTP POST Using PHP CURL


thanks, i’ll try it. Question? what are the codes to loop from drive until the last folder and subfolder.. and How could i get the id of a file..
Hi Jeremy,
Thanks for your response.
Display list of files within current and subdirectories (recursively) showing
< ?php
// grab a full file listing from the current and sub directories and show them as anchored
//(linked) files.
// pull a full file listing - requires the Unix 'find' and 'sort commands. 'find' will retrieve a
//list of all files from the current directory, 'sort' will sort the listing, and 'explode' will split
//all files into an array passed into $filelist.
$filelist = explode("\n",`find .|sort`);
// for each item (file) in the array...
for ($count=0;$count
VN:F [1.9.17_1161] please wait... Rating: 0.0/5 (0 votes cast)
// get the filename (including preceding directory, ie: ./software/gth1.0.9.tar.gz)
$filename=$filelist[$count];
// if it's not a directory, display linked
if (!is_dir($filename))
printf("%s
\n”,$filename,$filename);
// otherwise tell the user it’s a “category”
else printf(“
Category: %s
\n”,$filename);
}
?>
I think this will help you.