How to delete all directories and files in a specified directory _php tips

Source: Internet
Author: User

This article illustrates how PHP deletes all directories and files in a specified directory. Share to everyone for your reference. The implementation method is as follows:

<?php/* * Delete all directories and files (or specify files) in the specified directory * Extensible add some options (such as whether to delete the original directory, etc.) * Remove file sensitive operation use * @param $dir directory path * @param array $f
 ILE_TYPE Specifies the file type */function Delfile ($dir, $file _type= ') {if (Is_dir ($dir)) {$files = Scandir ($dir); Open the directory//list all the files in the directory and remove them. 
    And.. foreach ($files as $filename) {if ($filename!= '. ' && $filename!= ' ... ')
          {if (!is_dir ($dir.. $filename)) {if (Empty ($file _type)) {unlink ($dir. '/'. $filename); }else{if (Is_array ($file _type)) {//Regular matches the specified file if (Preg_match ($file _type[0), $file
              Name) {unlink ($dir. '/'. $filename);
                }else{//Specifies the file containing some strings if (False!=stristr ($filename, $file _type)) {
              Unlink ($dir. '/'. $filename);
          }}}else{Delfile ($dir. '/'. $filename);
        RmDir ($dir. '/'. $filename); }}}else{if (File_exists ($dir)) uNlink ($dir); } delfile (DirName (__file__), ' HTML ');

I hope this article will help you with your PHP programming.

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.