This time for everyone to bring PHP unlink and rmdir Delete directory within the file how to implement, PHP unlink and rmdir Delete directory files in the attention of the matter, the following is the actual case, together to see.
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 with the following code: Deldirandfile (' upload ');?>
Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!
Recommended reading:
Redis counter classes using steps
PHP binary Find algorithm case study