PHP Delete Directory

Source: Internet
Author: User

<?PHP/*Custom Delete functions that can delete files and recursively delete folders*/ functionMy_del ($path)//to customize the My_del function, the function has a parameter ($path). When calling the My_del () function, we also pass a path (e.g. ' PHP '){    if(Is_dir($path))//returns True if the file name exists and is a directory. If file is a relative path, its relative path is checked according to the current working directory.     {            $file _list=Scandir($path);//the Scandir () function returns an array that contains the files and directories in the specified path. If successful, returns an array, or false if it fails.             foreach($file _list  as $file)//iterate through each element in the array and loop through the code block            {                if($file! = '. ' &&$file! = ' ... ')//if the traversed path is not equal to. And the path is not equal to:{My_del ($path.‘ /‘.$file);//path of the original path Plus/plus traversal                }            }            @rmdir($path);//rmdir () function to delete empty directories    }    Else    {        @unlink($path);//unlink () function Delete file    } }  $path= ' php ';//folder path to delete//If the PHP file is not ANSI, but UTF-8 mode, and the folder to be deleted contains Chinese characters, the calling function needs to transcode//$path =iconv (' utf-8 ', ' gb2312 ', $path);My_del ($path);//returns the My_del function, and now all directories become emptyEcho"Initialization complete!!! ";/*What did Scandir get? <?phpprint_r (Scandir ("Images"));? > Output: Array ([0] = =. [ 1] = =. [2] = dog.jpg[3] = house.jpg[4] = logo.gif) So images/dog.jpg is its path.*/ 

PHP Delete Directory

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.