Home >Backend Development >PHP Tutorial >PHP路径操作类

PHP路径操作类

WBOY
WBOYOriginal
2016-06-23 13:14:501099browse

PHP路径操作类,命名方式仿照C#的System.IO.Path类。

<?phpclass Path{    /**     * 获取指定路径的目录部分     * */    public function GetDirectoryName($path){        return pathinfo($path,PATHINFO_DIRNAME);    }    /**     * 获取指定路径的文件名     * */    public static function GetFileName($path){        return pathinfo($path,PATHINFO_FILENAME);    }    /**     * 获取指定路径的文件名和扩展名     * */    public static function GetFileNameWithoutExtension($path){        return pathinfo($path,PATHINFO_BASENAME);    }    /**     * 获取指定路径的完整真实路径     * */    public static function GetFullPath($path){        return realpath($path);    }    /**     * 获取一个随机文件名     * */    public static function GetRandomFileName(){        return md5(uniqid(uniqid(),true));    }    /**     * 获取唯一临时文件名     * */    public static function GetTempFileName(){        return tempnam(sys_get_temp_dir (),'');    }    /**     * 获取临时目录     * */    public static function GetTempPath(){        return sys_get_temp_dir();    }    /**     * 判断是否存在扩展名     * */    public static function HasExtension($path){        $extension = pathinfo($path,PATHINFO_EXTENSION );        return empty($extension) === false;    }    /***     * 合并数组中的文件路径     * */    public static function Combine(array $paths){        $path = implode(DIRECTORY_SEPARATOR,array_values($paths));        $extension = pathinfo($path,PATHINFO_EXTENSION );        if(empty($extension) === false){            $path = chop($path,DIRECTORY_SEPARATOR);        }else{            $path = $path . DIRECTORY_SEPARATOR;        }        return $path;    }    public function __toString(){        return 'Path';    }}?>

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn