色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

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ù)。

php技術(shù)php目錄操作實(shí)例代碼,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 博客| 白朗县| 离岛区| 丹东市| 阿克陶县| 南岸区| 东平县| 蒙山县| 陕西省| 丹棱县| 巴林右旗| 赤峰市| 高碑店市| 盐边县| 襄城县| 清涧县| 自贡市| 定襄县| 蓬莱市| 陈巴尔虎旗| 青海省| 阜平县| 阳谷县| 和平县| 天水市| 吴江市| 和顺县| 济南市| 顺昌县| 巴里| 肥乡县| 双城市| 新营市| 临湘市| 宝丰县| 米脂县| 永嘉县| 合山市| 金塔县| 张家口市| 霍邱县|