php文件公用类

<?php

/**
 * 文件操作类
 * @author tangw
 */
class FileUtil {
    /*
     * @functionName:Directory
     * @param:str '/a/b/c/d/f'
     * @returnValue:true or false
     * @USAGE:Directory('/a/b/c/d/f');
     * @函数作用:生成目录....@_@..太精典了..
     */

    static public function directory($dir) {
        return is_dir($dir) or (self::directory(dirname($dir)) and mkdir($dir, 0777));
    }

    /**
     * 循环删除目录和文件方法
     * @author tangw
     * @param unknown_type $dirName
     */
    static public function delDirAndFile($dirName) {
        if ($handle = opendir("$dirName")) {
            while (false !== ( $item = readdir($handle) )) {
                if ($item != "." && $item != "..") {
                    //DIRECTORY_SEPARATOR PHP内部常量,表示路径分隔符
                    if (is_dir($dirName . DIRECTORY_SEPARATOR . $item)) {
                        delDirAndFile($dirName . DIRECTORY_SEPARATOR . $item);
                    } else {
                        if (unlink($dirName . DIRECTORY_SEPARATOR . $item)) {
                            //tracelog("成功删除文件: $dirName.DIRECTORY_SEPARATOR.$item<br />\n");
                        }
                    }
                }
            }
            closedir($handle);
            rmdir($dirName);
        }
    }

    static public function removeDir($dir) {
        if (is_dir($dir) && !is_link($dir)) {
            if ($dh = opendir($dir)) {
                while (($sf = readdir($dh)) !== false) {
                    if ('.' == $sf ¦¦ '..' == $sf) {
                        continue;
                    }
                    self::removedir($dir . DIRECTORY_SEPARATOR . $sf);
                }
                closedir($dh);
            }
            return rmdir($dir);
        }
        return @unlink($dir);
    }

    /**
     * <p>获取文件的后缀</p>
     * @param $file_name 文件名
     * @return
     */
    static public function getFileExt($file_name) {
        while ($dot = strpos($file_name, ".")) {
            $file_name = substr($file_name, $dot + 1);
        }
        return $file_name;
    }

    /**
     * 修改配置文件中变量值
     */
    static public function test_update_config_file() {
        $filename = "bbbbbbb";
        $dir = dirname(__FILE__);
        $path = $dir . "" . DIRECTORY_SEPARATOR . "config.php";
        echo $path;
        $contents = file_get_contents($path);
        $contents = modify_ini_value($contents, "theme", $filename);
        file_put_contents($path, $contents);
    }

    static public function modify_ini_value($content, $key, $replace) {
        $match = '/([ \t]*)(\$' . $key . '\s*=\s*\"?)(.*?)(\"?)(\s*;\s*$)/m';
        return (preg_replace($match, "\${1}\${2}{$replace}\${4}\${5}", $content));
    }

}

?>
posted @ 2012-06-29 00:28  赤脚大仙~  阅读(457)  评论(0编辑  收藏  举报
View Code