It's all right. Write the function that the directory wants to close
including traversing files under this folder, directory subdirectories read the current file under directories and files delete the directory subdirectories under the current folder and files above three functionsChinese files are not currently supported in Chinese
Copy CodeThe code is as follows:
Header ("Content-type:text/html;charset=utf-8");
/**
* Read the files and directories in the current directory
*
* @param string $path path
* @return array of all files that meet the criteria
*/
function Tlist ($path) {
$path = Iconv (' utf-8 ', ' GBK ', $path);
if (!is_dir ($path)) {
throw new Exception ($path. " Not a directory ");
}
$arr = Array (' dir ' =>array (), ' file ' =>array ());
$HD = Opendir ($path);
while (($file = Readdir ($HD))!==false) {
if ($file = = "." | | $file = = "..") {continue;}
if (Is_dir ($path. " /". $file)) {
$arr [' dir '] [] = Iconv (' GBK ', ' utf-8 ', $file);
}else if (Is_file ($path. " /". $file)) {
$arr [' file '] = iconv (' GBK ', ' utf-8 ', $file);
}
}
Closedir ($HD);
The echo "Directory is:". Implode ("
", $arr [' dir '])."
";
echo "files are:". Implode ("
", $arr [' file ']);
}
/**
* Traverse files and directories in the current directory and subfolders in the directory
*
* @param string $path path
* @return array of all files that meet the criteria
*/
function Blist ($path) {
if (!is_dir (Iconv ("Utf-8", "GBK", $path))) {
throw new Exception ("folder". $path. " Does not exist or is not a file ");
}
$arr = Array ();
$HD = Opendir (Iconv ("Utf-8", "GBK", $path));
while (($file = Readdir ($HD))!==false) {
if ($file = = "." | | $file = = "..") {continue;}
$newpath =iconv (' utf-8 ', ' GBK ', $path). ' /'. $file;
if (Is_dir ($newpath)) {
$arr [] = Blist ($path. " /". $file);
}else if (Is_file ($newpath)) {
$arr [] = Iconv (' GBK ', ' utf-8 ', $file);
}
}
Closedir ($HD);
return $arr;
}
/**
* Delete files and subdirectories under the directory
* #param string $path path
* #return String Delete succeeds return true failure returns false;
*/
function Dirdel ($path) {
if (!is_dir ($path)) {
throw new Exception ($path. " Input is not a valid directory ");
}
$hand = Opendir ($path);
while (($file = Readdir ($hand))!==false) {
if ($file = = "." | | $file = = "..") Continue
if (Is_dir ($path. " /". $file)) {
Dirdel ($path. " /". $file);
}else{
@unlink ($path. " /". $file);
}
}
Closedir ($hand);
@rmdir ($path);
}
?>
http://www.bkjia.com/PHPjc/326921.html www.bkjia.com true http://www.bkjia.com/PHPjc/326921.html techarticle It's all right. The functions that the directory wants to close include traversing files under the folder, directory subdirectories reading the current file under directories and files deleting the directory subdirectories under the current folder and ...