Directory traversal and directory deletion functions in PHP

Source: Internet
Author: User

It's okay, I wrote a list of functions I want to turn off.

Includes traversing files under this folder, directory subdirectories reading current files under directories and files delete directory subdirectories under current folder and files above three functions currently do not support Chinese document Chinese directory

<?php header ("Content-type:text/html;charset=utf-8"); /** * Read the files and directories under the current directory * * @param string $path path * @return array all files that meet the criteria/function tlist (   
    $path) {$path = Iconv (' utf-8 ', ' GBK ', $path); if (!is_dir ($path)) {throw new Exception ($path.)   
    Not a directory ");   
    $arr = Array (' dir ' =>array (), ' file ' =>array ());   
    $HD = Opendir ($path); while (($file = Readdir ($HD))!==false) {if ($file = = "." | | $file = = "...")   
        {continue;} if (Is_dir ($path.)   
        /". $file)) {$arr [' dir '] = iconv (' GBK ', ' utf-8 ', $file); }else if (Is_file ($path.)   
        /". $file)) {$arr [' file '] [] = Iconv (' GBK ', ' utf-8 ', $file);   
    } closedir ($HD); The echo "Directory has:". Implode ("<br/>", $arr [' dir ']). "   
    <br/> ";   
The echo "file has:". Implode ("<br/>", $arr [' file ']);  /** * traverse the files and directories in the current directory and the directory * http://www.bianceng.cn * @param string $path path * in subfolders @return  Array all eligible file/function Blist ($path) {if (!is_dir (Iconv ("Utf-8", "GBK", $path)) {throw new Excepti On (folder. $path. "   
   Does not exist or is not a document ");   
    } $arr = Array ();   
    $HD = Opendir (Iconv ("Utf-8", "GBK", $path)); while (($file = Readdir ($HD))!==false) {if ($file = = "." | | $file = = "...")   
          {continue;} $newpath =iconv (' utf-8 ', ' GBK ', $path). '   
        /'. $file; if (Is_dir ($newpath)) {$arr [] = Blist ($path. "   
        /". $file);   
        }else if (Is_file ($newpath)) {$arr [] = Iconv (' GBK ', ' utf-8 ', $file);   
    } closedir ($HD);   
return $arr;   
/** * Delete directory files and subdirectories * #param string $path path * #return String deletion succeeded returning true failed to return false; */function Dirdel ($path) {if (!is_dir ($path)) {throw new Exception ($path.)   
    Input is not a valid directory ");   
    } $hand = Opendir ($path); while (($file = Readdir ($hand))!==false) {if ($file = = "." | |  $file = = "...")   
        Continue if (Is_dir ($pAth. " /". $file)) {Dirdel ($path.)   
        /". $file); }else{@unlink ($path.)   
        /". $file);   
    } closedir ($hand);   
@rmdir ($path); }?>

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/PHP/

Related Article

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.