PHP unlink and rmdir delete directories and all file methods in the directory

Source: Internet
Author: User
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 ');? 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.