Function code: Deletes only the files in the specified directory, and does not delete the directory folder.
The code is as follows |
Copy Code |
Class Shanchu { All files under the looping 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 the file: $dirName/$item <br/>n"; } } } Closedir ($handle); } } } ?> Suppose you need to delete all the files in the directory named "Upload" (but you do not need to delete the directory folder), you can do this by using the following code: <?php Delfileunderdir (' upload '); ?> |
PHP Delete all directories
The code is as follows |
Copy Code |
Function deltree ($pathdir) { echo $pathdir;// if (Is_empty_dir) for debugging ($PATHDIR)//If empty { RM Dir ($pathdir);//delete } Else {///otherwise read this directory, except. Outer $d =dir ($pathdir); while ($a = $d->read ()) { If Is_file ($pathdir. ' /'. $a) && ($a!= '. ') && ($a!= ' ... ')) {unlink ($pathdir. ') /'. $a);} //If file is deleted 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 empty, delete RmDir directly ($pathdir. ') /'. $a); } } } $d->close (); Echo must first delete all files under the directory;//I debug with } Function Is_empty_dir ($pathdir) { //Determine whether the directory is empty $d =opend IR ($PATHDIR); $i = 0; while ($a =readdir ($d)) { $i + +; } Closedir ($d); if ($i >2) {return false;} Else return true; } |
PHP Delete directory and all files in directory
The code is as follows |
Copy Code |
<?php To recycle 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 the file: $dirName/$item <br/>n"; } } } Closedir ($handle); if (RmDir ($dirName)) echo "successfully deletes the directory: $dirName <br/>n"; } } Suppose you need to delete a sibling named "upload" that is all of the files in this directory, you can do this by using the following code: Deldirandfile (' upload '); ?> |