PHP Empty (delete) the file under the specified directory, do not delete the Directory folder implementation code, _php tutorial

Source: Internet
Author: User

PHP clears (deletes) the file under the specified directory, does not delete the Directory folder implementation code,


In web development, we may encounter the need to empty all files in a directory, but not delete subdirectories in this directory (and certainly not delete the deleted root directory). So how do you deal with this method of deleting a file without deleting the directory? The following bloggers will share with you a better solution to this problem. Look at the following function:

/* Delete the files under the specified directory, do not delete the directory folder */function delfile ($dirName) {if (file_exists ($dirName) && $handle =opendir ($dirName)) { while (false!== ($item = Readdir ($handle))) {if ($item! = "." && $item! = "...") {if (file_exists ($dirName. ') /'. $item) && Is_dir ($dirName. ' /'. $item)} {delfile ($dirName. ') /'. $item);} Else{if (Unlink ($dirName. ' /'. $item)) {return true;}}} Closedir ($handle);}}



http://www.bkjia.com/PHPjc/874632.html www.bkjia.com true http://www.bkjia.com/PHPjc/874632.html techarticle php Empty (delete) the file under the specified directory, do not delete the Directory folder implementation code, Web development we may encounter the need to be a directory of all files emptied, but not deleted ...

  • 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.