1: <?php
2: /**
3: * Pry Framework
4: *
5: * LICENSE
6: *
7: * This source file is subject to the new BSD license that is bundled
8: * with this package in the file LICENSE.txt.
9: *
10: * $Revision: 276 $
11: */
12:
13: /**
14: * Utilitaire pour fichier / dossier
15: * @category Pry
16: * @package File
17: * @version 1.1.0
18: * @author Olivier ROGER <oroger.fr>
19: *
20: *
21: */
22: class File_Util
23: {
24: /**
25: * Retourne l'extension d'un fichier avec ou sans le point
26: *
27: * @param string $file Nom de fichier
28: * @param bool $dot Afficher ou non le point dans l'extension
29: * @static
30: * @return string
31: */
32: public static function getExtension($file,$dot=false)
33: {
34: if(empty($file))
35: throw new InvalidArgumentException('Le nom de fichier ne peut être vide');
36:
37: $ext = pathinfo($file,PATHINFO_EXTENSION);
38: if($dot)
39: $ext = '.' . $ext;
40:
41: return $ext;
42: }
43:
44: /**
45: * Retourne le nom de fichier sans l'extension
46: * @todo Gérer les doubles extensions type tar.gz tout en gérant les nom de ficheirs avec des .
47: * @param string $file
48: * @return string
49: */
50: public static function getName($file)
51: {
52: if(empty($file))
53: throw new InvalidArgumentException('Le nom de fichier ne peut être vide');
54: else
55: $name = basename($file,strrchr($file,'.'));
56:
57: return $name;
58: }
59:
60: /**
61: * Retourne le nom dufichier/dossier compris dans un chemin
62: *
63: * @param string $path
64: * @return unknown
65: */
66: public static function getNameFromPath($path)
67: {
68: if(empty($path))
69: {
70: throw new InvalidArgumentException('Le nom de fichier ne peut être vide');
71: }
72: else
73: {
74: if(strrchr($path,'/'))
75: return strtolower(substr(strrchr($path,'/'),1));
76: else
77: return $path;
78: }
79: }
80:
81: /**
82: * Retourne la valeur en octet d'une taille courte (2K, 2M, 2G)
83: *
84: * @param string $size Taille courte. Accepte les unités K,M,G
85: * @static
86: * @return int
87: */
88: public static function getOctalSize($size)
89: {
90: if(empty($size))
91: return 0;
92: if(is_numeric($size))
93: return $size;
94: if(!is_numeric($size))
95: {
96: $unit = substr($size,-1);
97: if($unit == 'K')
98: $size *= 1<<10;
99: elseif($unit == 'M')
100: $size *= 1<<20;
101: elseif($unit == 'G')
102: $size *= 1<<30;
103: }
104: else
105: throw new InvalidArgumentException('La taille doit être une chaine');
106: return $size;
107: }
108:
109: /**
110: * Retourne une taille en octet sous forme lisible
111: *
112: * @param int $size
113: * @since 1.0.1
114: * @return string
115: */
116: public static function getHumanSize($size)
117: {
118: if(empty($size))
119: return 0;
120:
121: if($size<0)
122: {
123: $taille = '>2Go';
124: }
125: else
126: {
127: if($size < 1024)
128: $taille = round($size,3).' o';
129: elseif ($size< 1048576)
130: $taille = round($size/1024,3).' Ko';
131: elseif ($size< 1073741824)
132: $taille = round($size/1024/1024,3).' Mo';
133: elseif ($size< 1099511627776)
134: $taille = round($size/1024/1024/1024,3).' Go';
135: }
136: return $taille;
137: }
138: }
139: ?>