in PHP to delete files and directories is actually very simple as long as the two function of a unlink is a rmdir function, if you want to delete directory and file directory we need to use recursion to operate.

function code: only delete files under the specified directory, do not delete the directory folder, the code is as follows:

 class shanchu {/ / recycling directory of all files function delFileUnderDir ($dirName=../Smarty/templates/templates_c) {if ($handle = opendir ("$dirName")) {while (false = =! ($item = readdir ($handle))) {if ($item! = & & "." $item! = ".." {(is_dir) if ($dirName/$item)) {delFileUnderDir ("$dirName/$item");} else phpfensi.com if (unlink {// source code ("$dirName/$item")) echo "successfully deleted File: $dirName/$item< br /> n; closedir ($handle)}}}}};}

hypothesis need to delete all the files in a directory named "Upload", but no need to delete the directory folder, you can complete the following code:

 < PHP delFileUnderDir? (upload');? > 

php delete all code directory, as follows:

 function deltree ($pathdir) {echo $pathdir; / / debugging with the if (is_empty_dir ($pathdir)) / / if it is empty {rmdir ($pathdir); / / direct delete else {//} or read the directory, and $d=dir (except.. $pathdir (while); $a=$d-> (read)) {if (is_file ($pathdir.'/'.$a) & ($a; & & & ='.'!) ($a! ='..')) {unlink ($pathdir.'/'.$a);} / / if the file is directly deleted if (is_dir ($pathdir.'/'.$a) & ($a; & & & ='.'!) ($a) {// ='..'!) if the directory if (! Is_empty_dir ($pathdir.'/'.$a)) / / {// is empty if not call itself, but is the original path + his subordinate directory name deltree ($pathdir.'/'.$a);} if (is_empty_dir ($pathdir.'/'.$a) if it is empty) {// directly deleted rmdir ($pathdir.'/'.$a);}}}); echo $d-> close ("you must first remove all files" directory; / / I debugging with function is_empty_dir ($pathdir)}} {/ / judge directory is empty $d=opendir ($pathdir) while ($a=readdir; $i=0; ($d)) {$i++}; closedir ($d); if ($i> 2) {return false else return true;};}

PHP delete the directory and the directory of all files, the code is as follows:

 < PHP? / / delete the directory and file circulation function function delDirA NdFile ($dirName) {if ($handle = opendir ("$dirName")) {while (false = =! ($item = readdir ($handle))) {if ($item! = & & "". ".." $item! = {if (is_dir) ("$dirName/$item") {(delDirAndFile) the "$dirName/$item");} else {if (unlink ("$dirName/$item")) echo "successfully deleted files: $dirName/$item< br; /> n;}}} closedir ($handle); if (rmdir ($dirName)) echo" successfully deleted Directory: $dirName< br /> n;}} / / if need to delete a sibling directory called "Upload" that all the files in this directory, you can complete the following code: delDirAndFile (upload'); > 

> summarized above is small PHP unlink and rmdir delete directory and directory file code, I hope to help you, if you have any questions, please leave a message for me, Xiaobian will reply to you in time. Thank you for your support for the home website of the script!


This concludes the body part

This paper fixed link:http://www.script-home.com/php-unlink-and-rmdir-delete-all-the-file-instance-code-under-the-directory-and-directory.html | Script Home | +Copy Link

Article reprint please specify:PHP unlink and rmdir delete all the file instance code under the directory and directory | Script Home

You may also be interested in these articles!