PHP Directory Operation Instance Code

Source: Internet
Author: User

  This article mainly introduces the PHP directory operation instance code, need friends can refer to the following

The code is as follows: <?php     /**     * listdir    /    header ("content-type:text/h Tml;charset=utf-8 ");       $dirname = "./final/factapplication";       function Listdir ($dirname) {        $ds = Opendir ($dirname);     &NBSP ;   while (false!== ($file = Readdir ($ds)) {            $path = $dirname. ' /'. $file;             if ($file!= '. ' && $file!= ' ... ') {                if (Is_dir ($path)) {            & nbsp       Listdir ($path);                } else {                & nbsp   Echo $file. " <br> ";                            }        }         Closedir ($DS);    }     Listdir ($dirname);       Core: The classic application of recursion, as well as the basic operation of files and directories.   Code as follows: <?php    /**     * copydir    /    $srcdir = ". /fileupload ";     $dstdir = "B";       function copydir ($srcdir, $dstdir) {        mkdir ($dstdir);         $ds = Opendir ($srcdir);           while (false!== ($file = Readdir ($ds)) {            $p Ath = $srcdir. " /". $file;             $dstpath = $dstdir. " /". $file;             if ($file!= "." && $file!= "...") {                if (Is_dir ($path)) {            & nbsp       Copydir ($path, $dstpath);                } else {    &NBSP               copy ($path, $dstpath);                            }        }         Closedir ($DS);      }       copydir ($srcdir, $dstdir);     Core: copy function.     Code as follows: <?php    /**     * deldir    /      $dirname = ' a ';       function Deldir ($dirname) {        $ds = Opendir ($dirname);     &NBSP ;     while (false!== ($file = Readdir ($ds)) {            $path = $dirname. ' /'. $file;             if ($file!= '. ' && $file!= ' ... ') {                if (Is_dir ($path)) {            & nbsp       Deldir ($path);                } else {                    unlink ($path);                                     }         Closedir ($DS);           return rmdir ($dirname);    }       Deldir ($dirname);       Core: Note that unlink deletes the file with path.     Code as follows: <?php    /**     * dirsize    /      $dirname = "a" ;       function dirsize ($dirname) {       -static $tot;         $DS = Opendir ($dirname);         while (false!== ($file = Readdir ($ds)) {          $path = $ DirName. ' /'. $file;             if ($file!= '. ' && $file!= ' ... ') {              &nbsp if (Is_dir ($path)) {                    dirsize ($path);     &N Bsp          } else {                    $tot = $ Tot + filesize ($path);                            }                 return $tot;         Closedir ($DS);    }       echo dirsize ($dirname);     Core: To understand recursive functions by judging where $tot returns.
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.