Example 1
The code is as follows |
Copy Code |
function Del_dir ($dir) { { if (! $dir) {return;} $cacheDir = $dir; $DH = Opendir ($cacheDir); while ($file = Readdir ($DH)) { if ($file = = '. ') | | ($file = = ' ... ')) {continue;} if (file_exists ($cacheDir. ') /'. $file)) { if (Is_dir ($cacheDir. ') /'. $file)) { Del_dir ($cacheDir. ' /'. $file); }elseif (!unlink, $cacheDir. ' /'. $file)) { Finish delete operation } } } } } |
Example 2
code is as follows |
copy code |
? function Deldir ($dir) { Delete the files in the directory first: $DH =opendir ($dir); while ($file =readdir ($DH)) { if ($file!= "." && $file!= "...") { $fullpath = $dir. " /". $file; if (!is_dir ($fullpath)) { Unlink ($fullpath); } else { Deldir ($fullpath); } } }
Closedir ($DH); Delete current folder: if (RmDir ($dir)) { return true; } else { return false; } } ?> |
Example 3
The code is as follows |
Copy Code |
function RemoveDir ($dirName) { if (!is_dir ($dirName))//If the incoming parameter is not a directory, it is a file and should be deleted { @unlink ($dirName);//delete file return false; } $handle = @opendir ($dirName); If the passed in parameter is a directory, use Opendir to open the directory and assign the returned handle to the $handle while ($file = @readdir ($handle)!== false)//here explicitly tests whether the return value is equal (both the value and the type) false, otherwise any directory entry's name evaluates to False will cause the loop to stop (for example, a directory named "0 ”)。 { if ($file!= '. ') && $file!= ' ... ') In the file structure, you will include the form "." and ".." Upward structure, but they are not files or folders { $dir = $dirName. '/' . $file; Current file $dir as file directory + file Is_dir ($dir) RemoveDir ($dir): @unlink ($dir); Determine if the $dir is a directory, and if it is a directory, recursively call the RemoveDir ($dirName) function, delete the files and directories, and delete the file if it is not a directory } } Closedir ($handle);
return rmdir ($dirName); } |
Example 4
Delete a directory created a few days ago
The code is as follows |
Copy Code |
<?php function Delfile ($dir, $n)//delete all files created by n days before dir path; { if (Is_dir ($dir)) { if ($dh =opendir ($dir)) { while (false!== ($file = Readdir ($DH))) { if ($file!= "." && $file!= "...") { $fullpath = $dir. " /". $file; if (!is_dir ($fullpath)) { $filedate =date ("y-m-d", Filemtime ($fullpath)); $d 1=strtotime (Date ("y-m-d")); $d 2=strtotime ($filedate); $Days =round (($d 1-$d 2)/3600/24); if ($Days > $n) Unlink ($fullpath); deleting files
} } } } Closedir ($DH); } } ?> |