欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php目錄操作實(shí)例代碼

 更新時(shí)間:2014年02月21日 16:09:50   作者:  
這篇文章主要介紹了php目錄操作實(shí)例代碼,需要的朋友可以參考下

復(fù)制代碼 代碼如下:

<?php
    /**
    * listdir
    */
    header("content-type:text/html;charset=utf-8");

    $dirname = "./final/factapplication";

    function listdir($dirname) {
        $ds = opendir($dirname);
        while (false !== ($file = readdir($ds))) {
            $path = $dirname.'/'.$file;
            if ($file != '.' && $file != '..') {
                if (is_dir($path)) {
                    listdir($path);
                } else {
                    echo $file."<br>";
                }
            }
        }
        closedir($ds);
    }
    listdir($dirname);

核心:遞歸的經(jīng)典應(yīng)用,以及文件和目錄的基本操作。

復(fù)制代碼 代碼如下:

<?php
    /**
    * copydir
    */

    $srcdir = "../fileupload";
    $dstdir = "b";

    function copydir($srcdir, $dstdir) {
        mkdir($dstdir);
        $ds = opendir($srcdir);

        while (false !== ($file = readdir($ds))) {
            $path = $srcdir."/".$file;
            $dstpath = $dstdir."/".$file;
            if ($file != "." && $file != "..") {
                if (is_dir($path)) {
                    copydir($path, $dstpath);
                } else {
                    copy($path, $dstpath);
                }
            }
        }
        closedir($ds);

    }

    copydir($srcdir, $dstdir);

核心:copy函數(shù)。

復(fù)制代碼 代碼如下:

<?php
    /**
    * deldir
    */

    $dirname = 'a';

    function deldir($dirname) {
        $ds = opendir($dirname);

        while (false !== ($file = readdir($ds))) {
            $path = $dirname.'/'.$file;
            if($file != '.' && $file != '..') {
                if (is_dir($path)) {
                    deldir($path);
                } else {
                    unlink($path);
                }
            }
        }
        closedir($ds);

        return rmdir($dirname);
    }

    deldir($dirname);

核心:注意unlink刪除的是帶path的file。

復(fù)制代碼 代碼如下:

<?php
    /**
    * dirsize
    */

    $dirname = "a";

    function dirsize($dirname) {
        static $tot;
        $ds = opendir($dirname);
        while (false !== ($file = readdir($ds))) {
            $path = $dirname.'/'.$file;
            if ($file != '.' && $file != '..') {
                if(is_dir($path)) {
                    dirsize($path);
                } else {
                    $tot = $tot + filesize($path);
                }
            }
        }
        return $tot;
        closedir($ds);
    }

    echo dirsize($dirname);


核心:通過判斷$tot在哪里返回,理解遞歸函數(shù)。

相關(guān)文章

最新評(píng)論