deleting files and directories in PHP is actually very simple as long as two functions one is unlink a rmdir function, if you want to implement delete directories and directories under the file we need to use the recursive operation.
Function code: Delete Only the files under the specified directory, not the directory folder.
The code is as follows |
Copy Code |
Class Shanchu { All files in the loop directory function Delfileunderdir ($dirName = ": /smarty/templates/templates_c ") { if ($handle = Opendir ("$dirName")) { while (false!== ($item = Readdir ($handle))) { if ($item! = "." && $item! = "...") { if (Is_dir ("$dirName/$item")) { Delfileunderdir ("$dirName/$item"); } else { if (unlink ("$dirName/$item")) echo "successfully deleted file: $dirName/$item n "; } } } Closedir ($handle); } } } ?> Suppose you need to delete all the files in the directory named "Upload" (but without deleting the directory folder), you can do this with the following code: Delfileunderdir (' upload '); ?> |
PHP Delete all directories
code as follows |
copy code |
function deltree ($pathdir) { echo $pathdir;//Used for debugging if (Is_empty_dir ($pathdir))//If it is empty { RmDir ($pathdir);//delete directly } Else {//otherwise read this directory except for. and. Outside $d =dir ($pathdir); while ($a = $d->read ()) { if (Is_file ($pathdir. ' /'. $a) && ($a! = '. ') && ($a! = ' ... ')) {unlink ($pathdir. ') /'. $a);} If it's a file, delete it directly. if (Is_dir ($pathdir. ' /'. $a) && ($a! = '. ') && ($a! = ' ... ')) {//If the directory if (!is_empty_dir ($pathdir. ' /'. $a))//Is empty {//If not, call itself, but the original path + his subordinate directory name deltree ($pathdir. ' /'. $a); } if (Is_empty_dir ($pathdir. ' /'. $a)) {//If it is empty, delete it directly RmDir ($pathdir. ' /'. $a); } } } $d->close (); echo "must first delete all files in the directory";//I use it for debugging } } function Is_empty_dir ($pathdir) { Determine if the directory is empty $d =opendir ($pathdir); $i = 0; while ($a =readdir ($d)) { $i + +; } Closedir ($d); if ($i >2) {return false;} else return true; }
|
PHP Delete all files in directory and directory
The code is as follows |
Copy Code |
Loop Delete directory and file functions function Deldirandfile ($dirName) { if ($handle = Opendir ("$dirName")) { while (false!== ($item = Readdir ($handle))) { if ($item! = "." && $item! = "...") { if (Is_dir ("$dirName/$item")) { Deldirandfile ("$dirName/$item"); } else { if (unlink ("$dirName/$item")) echo "successfully deleted file: $dirName/$item n "; } } } Closedir ($handle); if (RmDir ($dirName)) echo "Successfully deleted directory: $dirName n "; } } Suppose you need to delete a sibling directory named "upload" that is all the files in this directory, which you can do with the following code: Deldirandfile (' upload '); ?> |
http://www.bkjia.com/PHPjc/444647.html www.bkjia.com true http://www.bkjia.com/PHPjc/444647.html techarticle deleting files and directories in PHP is actually very simple as long as two functions one is unlink a rmdir function, if you want to implement delete directories and directories under the file we need to use the recursive operation. ...