PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/io/FileExtend.php

http://gestion-calls.googlecode.com/
PHP | 464 lines | 238 code | 50 blank | 176 comment | 68 complexity | 87e71b192408b2e22f3b5df2a488dcc8 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, MIT, LGPL-3.0
  1. <?php
  2. // Require class
  3. // Find in oi package
  4. require_once('exception/FileException.php');
  5. // Require class
  6. // PEAR
  7. require_once('File/Util.php');
  8. /**
  9. * Class that provide functionalities to the abstraction of file and directory
  10. *
  11. * Based in java.oi.File with some adaptions
  12. *
  13. * @package io
  14. * @author Gustavo Gomes
  15. * @copyright 2006 Gustavo Gomes
  16. * @version 1.0
  17. * @see java.io.File
  18. */
  19. class FileExtend {
  20. //** Constants
  21. const separator = DIRECTORY_SEPARATOR;
  22. const separatorChar = DIRECTORY_SEPARATOR;
  23. const pathSeparator = PATH_SEPARATOR;
  24. const pathSeparatorChar = PATH_SEPARATOR;
  25. private static $FILE_WIN = "";
  26. const SORT_NONE = 0;
  27. const SORT_REVERSE = 1;
  28. const SORT_NAME = 2;
  29. const SORT_SIZE = 4;
  30. const SORT_DATE = 8;
  31. const SORT_RANDOM = 16;
  32. const LIST_FILES = 1;
  33. const LIST_DIRS = 2;
  34. const LIST_DOTS = 4;
  35. const LIST_ALL = 7;
  36. // Name of file or directory
  37. private $name = "";
  38. // file or directory path
  39. private $path = "";
  40. /**
  41. * FileExtend constructor
  42. *
  43. * <b>Overload</b>
  44. * FileExtend(filename : string)
  45. * FileExtend(parent : string, child : string)
  46. *
  47. * @param string $pathName
  48. * @param atring $name
  49. * @throws FileException
  50. */
  51. public function __construct($pathName,$name=null) {
  52. self::$FILE_WIN = defined('OS_WINDOWS') ? OS_WINDOWS : !strncasecmp(PHP_OS, 'win', 3);
  53. $parts = array();
  54. $path = "";
  55. // If has defined only the first parameter
  56. if ($pathName != "" && $name == null) {
  57. // get the separator type used
  58. $pathName = str_replace("\\",self::separator,$pathName);
  59. $pathName = str_replace("/",self::separator,$pathName);
  60. $parts = explode(self::separator,$pathName);
  61. $last = 1;
  62. if ($parts[count($parts)-1] == "")
  63. $last = 2;
  64. for ($i = 0;$i < (count($parts)-$last);$i++)
  65. $this->path .= $parts[$i].self::separator;
  66. $this->path .= $parts[$i];
  67. $this->name = $parts[$i];
  68. // If defined two parameters
  69. } else if ($name != "") {
  70. if ($name[strlen($name)-1] == "\\" || $name[strlen($name)-1] == "/")
  71. $name[strlen($name)-1] = "";
  72. $this->name = $name;
  73. if ($pathName[strlen($pathName)-1] != "\\" && $pathName[strlen($pathName)-1] != "/")
  74. $this->path = $pathName.self::separator.$name;
  75. else
  76. $this->path = $pathName.$name;
  77. } else {
  78. throw new FileException("File name is not defined.");
  79. }
  80. }
  81. /**
  82. * Get name file/directory
  83. *
  84. * @return string
  85. */
  86. public function getName() {
  87. return $this->name;
  88. }
  89. /**
  90. * Get file/directory path
  91. *
  92. * @return string
  93. */
  94. public function getPath() {
  95. return $this->path;
  96. }
  97. /**
  98. * Build path for web format (URI)
  99. *
  100. * @return string
  101. */
  102. public function buildPath() {
  103. return str_replace("\\","/",$this->path);
  104. }
  105. /**
  106. * Get parent directory for this path
  107. *
  108. * @return string
  109. */
  110. public function getParent() {
  111. return dirname($this->path);
  112. }
  113. public function getCompleteParent() {
  114. return dirname($this->path).self::separator;
  115. }
  116. /**
  117. * Get the file or directory absolute path
  118. *
  119. * @return string
  120. */
  121. public function getAbsolutePath() {
  122. return realpath($this->buildPath());
  123. }
  124. public function getAbsoluteDirectoryPath() {
  125. if ($this->isFile())
  126. return realpath(dirname($this->buildPath()));
  127. else
  128. return $this->getAbsolutePath();
  129. }
  130. public function isFile() {
  131. return is_file($this->buildPath());
  132. }
  133. public function isDirectory() {
  134. return is_dir($this->buildPath());
  135. }
  136. public function isDir() {
  137. return $this->isDirectory();
  138. }
  139. /**
  140. * Informate if this file or directory contain a absolute path
  141. *
  142. * @return boolean
  143. */
  144. public function isAbsolute() {
  145. $path = $this->path;
  146. if (preg_match('/(?:\/|\\\)\.\.(?=\/|$)/', $path)) {
  147. return false;
  148. }
  149. if (self::$FILE_WIN) {
  150. return preg_match('/^[a-zA-Z]:(\\\|\/)/', $path);
  151. }
  152. return ($path{0} == '/') || ($path{0} == '~');
  153. }
  154. public function isWritable() {
  155. return is_writable($this->buildPath());
  156. }
  157. public function canWrite() {
  158. return $this->isWritable();
  159. }
  160. public function isReadable() {
  161. return is_readable($this->buildPath());
  162. }
  163. public function canRead() {
  164. return $this->isReadable();
  165. }
  166. /**
  167. * Returns the file/directory length
  168. *
  169. * @return float
  170. */
  171. public function length() {
  172. return filesize($this->buildPath());
  173. }
  174. /**
  175. * Return true if it exists and false if it do not exists
  176. *
  177. * @return boolean
  178. */
  179. public function exists() {
  180. if ($this->isDirectory()) {
  181. return true;
  182. } else {
  183. if ($this->isFile() && file_exists($this->buildPath()))
  184. return true;
  185. else
  186. return false;
  187. }
  188. }
  189. /**
  190. * Rename this file/directory to file passed with parameter
  191. * This method do not only rename, but move current file to
  192. * file defined in parameter.
  193. *
  194. * @param FileExtend $destination
  195. * @throws FileException
  196. */
  197. public function renameTo($destination) {
  198. if (is_String($destination))
  199. return rename($this->buildPath(),$destination);
  200. else if (is_object($destination) && $destination instanceof FileExtend)
  201. return rename($this->buildPath(),$destination->getPath());
  202. else
  203. throw new FileExceptiob("Impossible rename this file with this parameter");
  204. }
  205. /**
  206. * Delete this file or directory case it exists
  207. *
  208. * @return boolean
  209. */
  210. public function delete() {
  211. if ($this->exists()) {
  212. if ($this->isDirectory())
  213. return @rmdir($this->buildPath());
  214. else if ($this->isFile())
  215. return @unlink($this->buildPath());
  216. else
  217. return false;
  218. } else {
  219. return false;
  220. }
  221. }
  222. /**
  223. * Create a directory based in this abstract pathname
  224. *
  225. * @return boolean
  226. */
  227. public function mkdir() {
  228. return @mkdir($this->buildPath(),0777);
  229. }
  230. /**
  231. * Create a directory and your parents if necessary,
  232. * based in this abstract pathname
  233. *
  234. * @return boolean
  235. */
  236. public function mkdirs() {
  237. $dirs = explode("/",$this->buildPath());
  238. $currentDir = "";
  239. for ($i = 0;$i < count($dirs);$i++) {
  240. $currentDir .= $dirs[$i].self::separator;
  241. $dir = new FileExtend($currentDir);
  242. if (!$dir->exists()) {
  243. if (!$dir->mkdir())
  244. return false;
  245. }
  246. }
  247. return true;
  248. }
  249. /**
  250. * Get file or directory last modifier
  251. *
  252. * @return string - date on standard [yyyy-mm-dd hh:mm:ss]
  253. */
  254. public function lastModified() {
  255. if ($this->exists())
  256. return date("Y-m-d H:i:s",filemtime($this->buildPath()));
  257. }
  258. /**
  259. * Create an array bidimensional with files and directories that directory
  260. * Case directory don't exists or this FileExtend class don't a directory then return null
  261. *
  262. * Example:
  263. * $file = new FileExtend("path/something");
  264. * print_r($file->listFilesName());
  265. *
  266. * Output:
  267. * array ([0] => array([name] => ex.js, [size] => 2015, [date] => 10251226485),
  268. * [1] => array([name] => ex2.js, [size] => 1000, [date] => 10251226485))
  269. *
  270. * @param int $list
  271. * @param int $sort
  272. * @return array
  273. */
  274. public function listFilesName($list=null, $sort=null) {
  275. if ($this->exists() && $this->isDir()) {
  276. if ($list == null)
  277. $list = self::LIST_ALL;
  278. if ($sort == null)
  279. $sort = self::SORT_NAME;
  280. $files = File_Util::listDir($this->buildPath(),$list,$sort,null);
  281. for ($i = 0;$i < count($files);$i++)
  282. $filesName[] = $files[$i]->name;
  283. return $filesName;
  284. } else {
  285. return null;
  286. }
  287. }
  288. /**
  289. * Same implemetation of the method above, but
  290. * insteand of common array for each file, this method return
  291. * a type FileExtend for each file or directory.
  292. *
  293. * Example:
  294. * $file = new FileExtend("path/something");
  295. * $list = $file->listFiles();
  296. * if ($list != null)
  297. * echo $list[0]->getName().$list[0]->length();
  298. *
  299. * @param int $list
  300. * @param int $sort
  301. * @return array
  302. */
  303. public function listFiles($list=null, $sort=null) {
  304. if ($this->exists() && $this->isDir()) {
  305. $filesObj = array();
  306. if ($list == null)
  307. $list = self::LIST_ALL;
  308. if ($sort == null)
  309. $sort = self::SORT_NAME;
  310. $files = File_Util::listDir($this->buildPath(),$list,$sort,null);
  311. for ($i = 0;$i < count($files);$i++)
  312. $filesObj[] = new FileExtend($this->buildPath(),$files[$i]->name);
  313. return $filesObj;
  314. } else {
  315. return null;
  316. }
  317. }
  318. /**
  319. * Get some file or directory informations
  320. *
  321. * @return array
  322. */
  323. public function getInformation() {
  324. if ($this->exists()) {
  325. $stat = stat($this->buildPath());
  326. $statReturn['modify'] = date("Y-m-d H:i:s",$stat['mtime']);
  327. $statReturn['change'] = date("Y-m-d H:i:s",$stat['ctime']);
  328. $statReturn['access'] = date("Y-m-d H:i:s",$stat['atime']);
  329. $statReturn['size'] = $stat['size'];
  330. return $statReturn;
  331. } else {
  332. return null;
  333. }
  334. }
  335. /**
  336. * Alises for the method above
  337. *
  338. * @return array
  339. */
  340. public function getInfo() {
  341. return $this->getInformation();
  342. }
  343. /**
  344. * Get file extension
  345. *
  346. * @return string
  347. */
  348. public function getExtension() {
  349. if ($this->isDirectory())
  350. return "folder";
  351. $parts = explode(".",$this->name);
  352. return $parts[count($parts)-1];
  353. }
  354. /**
  355. * Get the usable space in disk
  356. *
  357. * @return int
  358. */
  359. public function getUsableSpace() {
  360. return ($this->getTotalSpace() - $this->getFreeSpace());
  361. }
  362. /**
  363. * Get free space in disk
  364. *
  365. * @return int
  366. */
  367. public function getFreeSpace() {
  368. return disk_free_space($this->getAbsolutePath());
  369. }
  370. /**
  371. * Get total disk space
  372. *
  373. * @return int
  374. */
  375. public function getTotalSpace() {
  376. return disk_total_space($this->getAbsolutePath());
  377. }
  378. /**
  379. * Get total space in directory
  380. *
  381. * If the file has been a directory return your total size
  382. * If the file has been a file, return total size of the directory that
  383. * it be.
  384. *
  385. * @return int
  386. */
  387. public function getDirectoryTotalSpace() {
  388. $size = 0;
  389. if ($this->isDirectory())
  390. $this->getSizeOfDirectory($size,$this);
  391. else
  392. $this->getSizeOfDirectory($size,new FileExtend($this->getAbsoluteDirectoryPath()));
  393. return $size;
  394. }
  395. private function getSizeOfDirectory(&$size,$dirs) {
  396. $files = $dirs->listFiles();
  397. for ($i = 0;$i < count($files);$i++) {
  398. if ($files[$i]->getName() != "." && $files[$i]->getName() != "..") {
  399. $size += $files[$i]->length();
  400. if ($files[$i]->isDirectory())
  401. $this->getSizeOfDirectory($size,$files[$i]);
  402. }
  403. }
  404. }
  405. /**
  406. * For echo this class objects
  407. *
  408. * @return string
  409. */
  410. public function __toString() {
  411. return $this->path;
  412. }
  413. }
  414. ?>