PHP路径操作类,命名方式仿照C#的System.IO.Path类。
1<?php 2class Path{ 3 /** 4 * 获取指定路径的目录部分 5 * */ 6 public function GetDirectoryName($path){ 7 return pathinfo($path,PATHINFO_DIRNAME); 8 } 9 /** 10 * 获取指定路径的文件名 11 * */ 12 public static function GetFileName($path){ 13 return pathinfo($path,PATHINFO_FILENAME); 14 } 15 /** 16 * 获取指定路径的文件名和扩展名 17 * */ 18 public static function GetFileNameWithoutExtension($path){ 19 return pathinfo($path,PATHINFO_BASENAME); 20 } 21 /** 22 * 获取指定路径的完整真实路径 23 * */ 24 public static function GetFullPath($path){ 25 return realpath($path); 26 } 27 /** 28 * 获取一个随机文件名 29 * */ 30 public static function GetRandomFileName(){ 31 return md5(uniqid(uniqid(),true)); 32 } 33 /** 34 * 获取唯一临时文件名 35 * */ 36 public static function GetTempFileName(){ 37 return tempnam(sys_get_temp_dir (),''); 38 } 39 /** 40 * 获取临时目录 41 * */ 42 public static function GetTempPath(){ 43 return sys_get_temp_dir(); 44 } 45 /** 46 * 判断是否存在扩展名 47 * */ 48 public static function HasExtension($path){ 49 $extension = pathinfo($path,PATHINFO_EXTENSION ); 50 return empty($extension) === false; 51 } 52 /*** 53 * 合并数组中的文件路径 54 * */ 55 public static function Combine(array $paths){ 56 $path = implode(DIRECTORY_SEPARATOR,array_values($paths)); 57 $extension = pathinfo($path,PATHINFO_EXTENSION ); 58 if(empty($extension) === false){ 59 $path = chop($path,DIRECTORY_SEPARATOR); 60 }else{ 61 $path = $path . DIRECTORY_SEPARATOR; 62 } 63 return $path; 64 } 65 public function __toString(){ 66 return 'Path'; 67 } 68} 69?>