In php, you can use DirectoryIterator to obtain the files or directories in the specified directory. $ pathtmp; $ Paths ($ path); foreach ($ oDiras $ file) in php. DirectoryIterator and RecursiveDirectoryIterator
In php, you can use DirectoryIterator to obtain the files or directories in 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 obtains all the files in the directory, including subdirectories:
Use RecursiveIteratorIterator.
(RecursiveIteratorIterator is a recursive iterator, which can be followed by four parameters (only one)
RecursiveIteratorIterator: LEAVES_ONLY
Default: Used in _ construct
This function is used to remove branches and leaves, skip empty nodes, and recursively retrieve real values.
For example
1. when a recursive folder retrieves a file, the folder itself is skipped. only the files under the folder are taken. all output items are file (files and files in subfolders at all levels)
2. the key of the first few dimensions is skipped for multi-dimensional arrays, and the value is not an array for each output item.
3. XML only takes values (text) without outputting the node name. of course, it depends on what content you set to get xml.
RecursiveIteratorIterator: SELF_FIRST
All items are included. for example, recursive folders are output together with subfolders. The order is parent and child
RecursiveIteratorIterator: CHILD_FIRST
Same as above, but the order is parent after child,./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] = & gt; 1222049895
)
[1] => Array
(
[Link] =>/tmp/. font-unix
[Name] => fs7100
[Type] => dir
[Size] => 0b
[Mtime] = & gt; 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 ))
{
Throw new Exception ('you do 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 are all done let the user know ***/
Echo 'renaming of files complete'. "\ n ";
}
Catch (Exception $ e)
{
Echo $ e-> getMessage (). "\ n ";
}
?>