Directoryiterator and Recursivedirectoryiterator in PHP
In PHP, you can use Directoryiterator to get the file or directory of the specified directory.
$path = "/tmp"; $oDir = new Directoryiterator ($path); foreach ($oDir as $file) { if ($file->isfile ()) { $tmpFile [' link '] = $file->getpath (); $tmpFile [' name '] = $file->getfilename (); $tmpFile [' type '] = ' file '; $tmpFile [' size '] = _cal_size ($file->getsize ()); $tmpFile [' mtime '] = $file->getmtime (); $arrFile [] = $tmpFile; } } Print_r ($arrFile); /* Output Array ( [0] = = Array ( [link] =/tmp [name] = + Scim-panel-socket-:0-root [ Type] = dir [size] = 0b [mtime] = 1222049895 ) [1] = = Array ( [link] = /tmp [name] +. Font-unix [Type] = dir [size] = 4k [Mtime] = 1241417372 ) */
Recursivedirectoryiterator gets all the files in the directory, including subdirectories, which are paired with:
Recursiveiteratoriterator use.
(Recursiveiteratoriterator is a recursive iterator that can be followed with four parameters (only any)
Recursiveiteratoriterator::leaves_only
By default, the usage is set in the __construct
The function is to leave the leaves of the branches, skip the empty nodes, and recursively take the real value.
Example is
1. Recursive folder to skip the folder itself, only the folder under the file, the output of all files (files and subfolders of all levels)
2. Multi-dimensional arrays skip the first few dimensions of key, and value, each output is not an array
3.XML only value (text), do not output node name, of course, depending on your settings to get the XML what content
Recursiveiteratoriterator::self_first
Items, such as a recursive folder, are also exported as a subfolder name, in the order that the
Recursiveiteratoriterator::child_first
Ditto, but the order is the first son stepfather,./test/test.php will be in front of./test (folder)
$path = "/tmp/";
$objects = new Recursiveiteratoriterator (new Recursivedirectoryiterator ($path));
foreach ($objects as $object)
{
$tmpFile [' link '] = $object->getpath ();
$tmpFile [' name '] = $object->getfilename ();
$tmpFile [' type '] = $object->isfile ()? ' File ': ' dir ';
$tmpFile [' size '] = _cal_size ($object->getsize ());
$tmpFile [' mtime '] = $object->getmtime ();
$arrFile [] = $tmpFile;
}
Print_r ($arrFile);
/*
Output
Array
(
[0] = = Array
(
[link] =/tmp
[Name] = Scim-panel-socket-:0-root
[Type] = Dir
[Size] = 0b
[Mtime] = 1222049895
)
[1] = = Array
(
[link] =/tmp/.font-unix
[Name] = fs7100
[Type] = Dir
[Size] = 0b
[Mtime] = 1241417372
)
)
*/
Let's look at an example:
/*** the target directory, no trailling slash ***/
$directory = './';
Try
{
/*** Check if we have a valid directory ***/
if (!is_dir ($directory))
{
throw new Exception (' Directory does not exist! '. " \ n ");
}
/*** Check if we have permission to rename the files ***/
if (!is_writable ($directory))
{
The throw new Exception (' You does not have renaming permissions! '. \ n ");
}
/**
*
* @collapse white space
*
* @param string $string
*
* @return String
*
*/
function Collapsewhitespace ($string)
{
Return preg_replace ('/\s+/', ', $string);
}
/**
* @convert file names to Nice names
*
* @param string $filename
*
* @return String
*
*/
function Safe_names ($filename)
{
$filename = Collapsewhitespace ($filename);
$filename = Str_replace (', '-', $filename);
$filename = Preg_replace ('/[^a-z0-9-.] /I ', ', $filename);
Return Strtolower ($filename);
}
$it = new Recursiveiteratoriterator (new Recursivedirectoryiterator ($directory, 0));
/*** loop directly over the object ***/
while ($it->valid ())
{
/*** Check if value is a directory ***/
if (! $it->isdot ())
{
if (!is_writable ($directory. ' /'. $it->getsubpathname ()))
{
Echo ' Permission Denied: '. $directory. ' /'. $it->getsubpathname (). " \ n ";
}
Else
{
/*** the old file name ***/
$old _file = $directory. ' /'. $it->getsubpathname ();
/*** the new file name ***/
$new _file = $directory. ' /'. $it->getsubpath (). ' /'. Safe_names ($it->current ());
/*** Rename the file ***/
Rename ($old _file, $new _file);
/*** a little message to say file is converted ***/
Echo ' Renamed '. $directory. ' /'. $it->getsubpathname (). " \ n ";
}
}
/*** move to the next iteration ***/
$it->next ();
}
/*** when we ' all do let the user know ***/
echo ' Renaming of Files complete '. \ n ";
}
catch (Exception $e)
{
echo $e->getmessage (). " \ n ";
}
?>