<? Php Tutorial
/**
* Convert/
*
* @ Param string $ path
* @ Return string path
*/
Function dir_path ($ path ){
$ Path = str_replace ('\', '/', $ path );
If (substr ($ path,-1 )! = '/') $ Path = $ path .'/';
Return $ path;
}
/**
* Create a directory
*
* @ Param string $ path
* @ Param string $ mode attribute
* @ Return string returns true if it already exists; otherwise, it is flase.
*/
Function dir_create ($ path, $ mode = 0777 ){
If (is_dir ($ path) return TRUE;
$ Ftp_enable = 0;
$ Path = dir_path ($ path );
$ Temp = explode ('/', $ path );
$ Cur_dir = '';
$ Max = count ($ temp)-1;
For ($ I = 0; $ I <$ max; $ I ++ ){
$ Cur_dir. = $ temp [$ I]. '/';
If (@ is_dir ($ cur_dir) continue;
@ Mkdir ($ cur_dir, 0777, true );
@ Chmod ($ cur_dir, 0777 );
}
Return is_dir ($ path );
}
/**
* Copy the directory and all the files below
*
* @ Param string $ fromdir original path
* @ Param string $ todir target path
* @ Return string if the target path does not exist, false is returned; otherwise, true is returned.
*/
Function dir_copy ($ fromdir, $ todir ){
$ Fromdir = dir_path ($ fromdir );
$ Todir = dir_path ($ todir );
If (! Is_dir ($ fromdir) return FALSE;
If (! Is_dir ($ todir) dir_create ($ todir );
$ List = glob ($ fromdir .'*');
If (! Empty ($ list )){
Foreach ($ list as $ v ){
$ Path = $ todir. basename ($ v );
If (is_dir ($ v )){
Dir_copy ($ v, $ path );
} Else {
Copy ($ v, $ path );
@ Chmod ($ path, 0777 );
}
}
}
Return TRUE;
}
/**
* Convert all file encoding formats in the directory
*
* @ Param string $ in_charset original character set
* @ Param string $ out_charset target character set
* @ Param string $ dir directory address
* @ Param string $ File format converted by fileexts
* @ Return string if the original and target character sets are the same, false is returned; otherwise, true is returned.
*/
Function dir_iconv ($ in_charset, $ out_charset, $ dir, $ fileexts = 'php | html | htm | shtml | shtm | js | txt | XML '){
If ($ in_charset ==$ out_charset) return false;
$ List = dir_list ($ dir );
Foreach ($ list as $ v ){
If (preg_match ("/. ($ fileexts)/I", $ v) & is_file ($ v )){
File_put_contents ($ v, iconv ($ in_charset, $ out_charset, file_get_contents ($ v )));
}
}
Return true;
}
/**
* List all files in the directory
*
* @ Param string $ path
* @ Param string $ exts extension
* @ Param array $ list the list of added files
* @ Return array all files that meet the conditions
*/
Function dir_list ($ path, $ exts = '', $ list = array ()){
$ Path = dir_path ($ path );
$ Files = glob ($ path .'*');
Foreach ($ files as $ v ){
$ Fileext = fileext ($ v );
If (! $ Exts | preg_match ("/. ($ exts)/I", $ v )){
$ List [] = $ v;
If (is_dir ($ v )){
$ List = dir_list ($ v, $ exts, $ list );
}
}
}
Return $ list;
}
/**
* Set the access time and modification time for all files under the Directory
*
* @ Param string $ path
* @ Param int $ mtime modification time
* @ Param int $ atime access time
* @ Return if array is not a directory, false is returned; otherwise, true is returned.
*/
Function dir_touch ($ path, $ mtime = TIME, $ atime = TIME ){
If (! Is_dir ($ path) return false;
$ Path = dir_path ($ path );
If (! Is_dir ($ path) touch ($ path, $ mtime, $ atime );
$ Files = glob ($ path .'*');
Foreach ($ files as $ v ){
Is_dir ($ v )? Dir_touch ($ v, $ mtime, $ atime): touch ($ v, $ mtime, $ atime );
}
Return true;
}
/**
* Directory list
*
* @ Param string $ dir path
* @ Param int $ parentid parent id
* @ Param array $ directory input by dirs
* @ Return array returns the directory list.
*/
Function dir_tree ($ dir, $ parentid = 0, $ dirs = array ()){
Global $ id;
If ($ parentid = 0) $ id = 0;
$ List = glob ($ dir .'*');
Foreach ($ list as $ v ){
If (is_dir ($ v )){
$ Id ++;
$ Dirs [$ id] = array ('id' => $ id, 'parentid' => $ parentid, 'name' => basename ($ v ), 'dir' => $ v. '/');
$ Dirs = dir_tree ($ v. '/', $ id, $ dirs );
}
}
Return $ dirs;
}
/**
* Delete all files in the directory and Directory
*
* @ Param string $ dir path
* @ Return bool returns TRUE if the call succeeds, and FALSE if the call fails.
*/
Function dir_delete ($ dir ){
$ Dir = dir_path ($ dir );
If (! Is_dir ($ dir) return FALSE;
$ List = glob ($ dir .'*');
Foreach ($ list as $ v ){
Is_dir ($ v )? Dir_delete ($ v): @ unlink ($ v );
}
Return @ rmdir ($ dir );
}
?>