PHP Recursive Delete folder

Source: Internet
Author: User

The PHP recursive delete folder function was written two times. The first execution did not delete successfully, need to delete successfully ... So it should be a failure, the code is as follows:


<span style= "color: #FF0000;" >//Delete File method Private function Rmdirs ($val) {rmdir ($val); if (!file_exists ($val)) {return false;} if (Is_file ($val) | | Is_link ($val)) {return unlink ($val);} if (Is_dir ($val)) {if ($dir = Opendir ($val)) {while ($file = Readdir ($dir)) {if ($file = = ". ' | | $file = = ' ... ') {continue;} $path = $val. '/'. $file; unlink ($path); $this->rmdirs ($path);} Closedir ($val);}} --></span>

The correct code is as follows:

/** * Recursive deletion of files * $path    Incoming folder addresses to be deleted */<span style= "color: #33CC00;" >function Deldir ($path) {if (Is_dir ($path)) {$file _list = Scandir ($path); foreach ($file _list as $file) {if ($file! = '). && $file! = ' ... ') {Deldir ($path. ') /'. $file);//recursively delete echo ' <font color= ' Blue > '. $path. ' /'. $file. ' File cleanup succeeded!</font><br> ';}} @rmdir ($path);//Delete empty directory}else{@unlink ($path);//delete file}}</span>


PHP Recursive Delete folder

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.