Overview

Namespaces

  • None
  • PHP
  • Pry
    • Auth
      • Interfaces
    • Config
    • Controller
    • Date
    • Db
    • Feed
      • Abstracts
      • Writers
    • File
      • Decorator
    • Form
      • Element
    • Image
    • Log
      • Writer
    • Net
      • Exception
    • Session
    • Util
    • Validate
      • Validator
    • View

Classes

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