How PHP unlink and rmdir delete files in a directory

Source: Internet
Author: User
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

Related Article

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.