PHP using recursive implementation to delete file directory _php tips

Source: Internet
Author: User

Delete directory directly, if it is empty directory can be deleted, if not empty directory, this time can only delete the files in the directory, and then delete the directory. I encapsulated a delete function and then called the function directly. Like can be directly used, coding is GBK, use the attention of the code.

<?php
/**
 * Created by Phpstorm.
 * User:administrator
 * DATE:2016/9/10 0010 * time:20:27//
 /delete file, delete file internal file, delete
folder
header (" content-type:text/html; CHARSET=GBK ");
function Deldir ($dir) {
 $dh =opendir ($dir);
 while ($file =readdir ($DH)) {
 if ($file!= "." && $file!= "...") {//Judge whether this directory and superior directory
 if (!is_dir ($dir.) /". $file)) {
  unlink ($dir.) /". $file);
 } else{
  //Recursive
  Deldir ($dir. " /". $file);
 }} Closedir ($DH);
 if (RmDir ($dir)) {return
 true;
 } else{return
 false;
 }
 Delete Function End
 if (Deldir ("test")) {
 echo ' delete file succeeded ';
 Die ();
 } else
 Echo ' delete file failed ';
 Die ();

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Recommend a wonderful topic about PHP recursive operation: PHP recursive operation

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.