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. This article mainly introduces the PHP Unlink and rmdir Delete directory and directory of all the file instance code, the need for friends can refer to, hope to help everyone.
Function code: Delete Only the files under the specified directory, do not delete the directory folder, the code is as follows:
Class Shanchu all file function delfileunderdir ($dirName = ") in the {///cyclic directory. /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 {//Open source phpfensi.com if (unlink ("$dirNam e/$item ")) echo" Successfully deleted files: $dirName/$item <br/>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:
<?php delfileunderdir (' upload ');? >
PHP deletes all directories with the following code:
function deltree ($pathdir) {echo $pathdir;//When debugging with if (Is_empty_dir ($pathdir))//If empty {rmdir ($pathdir);//directly Delete} else {// Otherwise read this directory except for. and. External $d =dir ($pathdir); while ($a = $d->read ()) {if (Is_file ($pathdir. ') /'. $a) && ($a! = '. ') && ($a! = ' ... ')) {unlink ($pathdir. ') /'. $a);} If it is a file, delete the IF (Is_dir ($pathdir) directly. /'. $a) && ($a! = '. ') && ($a! = ' ... ')) {//If it is a 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 rmdir ($pathdir. ' /'. $a); }}} $d->close (); echo "must first delete all files in the directory";//I Debug with}} function Is_empty_dir ($pathdir) {//To 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 directories and directories under All files, the code is as follows:
<?php//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 "Succeeded Delete file: $dirName/$item <br/>n "; }}} closedir ($handle); if (RmDir ($dirName)) echo "Successfully deleted directory: $dirName <br/>n"; }}//Suppose you need to delete a sibling directory named "upload" that is all the files in this directory, which you can do by following the code: Deldirandfile (' upload ');?