/lib/FileSystem.php

https://bitbucket.org/ponikrf/vfileviewer · PHP · 168 lines · 96 code · 16 blank · 56 comment · 9 complexity · 7f6eccee7f56557333b01ee7d29983b6 MD5 · raw file

  1. <?php
  2. /**
  3. * Class to operate the file system
  4. *
  5. * Unix only
  6. *
  7. * @todo Add windows system support
  8. * @author V team
  9. * @category Components
  10. * @package vbilling.core.components
  11. * @subpackage core
  12. * @version 0.0.2
  13. */
  14. namespace VLibrary\Core;
  15. use \Exception;
  16. class FileSystem
  17. {
  18. /**
  19. * @var переменная будет содержать в массиве другие переменные необходимые для передачи
  20. * */
  21. private $_localStorage = array();
  22. /**
  23. * scan directory and return array
  24. * ('files' => $files, 'dirs' => $dirs)
  25. *
  26. * @todo recursive
  27. *
  28. * @access public
  29. * @param string directory path
  30. * @return array see header
  31. * */
  32. public function scanDir($dirPath)
  33. {
  34. $path = realpath($dirPath);
  35. $path = $path;
  36. $files = array();
  37. $filesInfo = array();
  38. $directory = array();
  39. if ($fp = @opendir($path))
  40. {
  41. while (FALSE !== ($file = readdir($fp)))
  42. {
  43. if (is_dir($path.DIRECTORY_SEPARATOR.$file) && strncmp($file, '.', 1) !== 0)
  44. {
  45. $directory[] = $file;
  46. }
  47. elseif (strncmp($file, '.', 1) !== 0)
  48. {
  49. $files[] = $file;
  50. $filesInfo[$file] = $this -> getFileInfo($path.DIRECTORY_SEPARATOR.$file);
  51. }
  52. }
  53. return array('files' => $files,'dirs' => $directory,'filesinfo' => $filesInfo);
  54. }
  55. else
  56. {
  57. return FALSE;
  58. }
  59. }
  60. /**
  61. * give a file info
  62. *
  63. * @access public
  64. * @param string path to file
  65. * @param array return file param array
  66. * @return array file info array
  67. * */
  68. public function getFileInfo($filePath, $returned_values = array('name', 'server_path', 'size', 'date'))
  69. {
  70. if ( ! file_exists($filePath))
  71. throw new Exception("file '$filePath' not found ");
  72. if (is_string($returned_values))
  73. $returned_values = explode(',', $returned_values);
  74. foreach ($returned_values as $key)
  75. {
  76. switch ($key)
  77. {
  78. case 'name':
  79. $fileinfo['name'] = substr(strrchr($filePath, DIRECTORY_SEPARATOR), 1);
  80. break;
  81. case 'server_path':
  82. $fileinfo['server_path'] = $filePath;
  83. break;
  84. case 'size':
  85. $fileinfo['size'] = filesize($filePath);
  86. break;
  87. case 'date':
  88. $fileinfo['date'] = filemtime($filePath);
  89. break;
  90. case 'readable':
  91. $fileinfo['readable'] = is_readable($filePath);
  92. break;
  93. case 'writable':
  94. // There are known problems using is_weritable on IIS. It may not be reliable - consider fileperms()
  95. $fileinfo['writable'] = is_writable($filePath);
  96. break;
  97. case 'executable':
  98. $fileinfo['executable'] = is_executable($filePath);
  99. break;
  100. case 'fileperms':
  101. $fileinfo['fileperms'] = fileperms($filePath);
  102. break;
  103. }
  104. }
  105. return $fileinfo;
  106. }
  107. /**
  108. * Convert byte to round byte system
  109. *
  110. * @access public
  111. * @param string bytes
  112. * @param int precision
  113. * @return string true or false
  114. * */
  115. function roundFileSize($bytes, $precision = 2) {
  116. $units = array('B', 'KB', 'MB', 'GB', 'TB');
  117. $bytes = max($bytes, 0);
  118. $pow = floor(($bytes?log($bytes):0)/log(1024));
  119. $pow = min($pow, count($units)-1);
  120. $bytes /= pow(1024, $pow);
  121. return round($bytes, $precision).' '.$units[$pow];
  122. }
  123. /**
  124. * Return extension of a file name
  125. *
  126. * @access public
  127. * @param string file name
  128. * @return bool true or false
  129. * */
  130. public function getFileExtension($fileName)
  131. {
  132. $_filename_array = explode('.', $filename);
  133. if (count($_filename_array)>1)
  134. return end($_filename_array);
  135. else
  136. return '';
  137. }
  138. /**
  139. * Checks Correctly file name
  140. *
  141. * @access public
  142. * @param string file name
  143. * @return bool true or false
  144. * */
  145. public function chackFileName($fileName)
  146. {
  147. if (preg_match("/(^[a-zA-Z0-9]+([a-zA-Z\_0-9\.-]*))$/" , $fileName) AND strlen($fileName) < 255)
  148. return TRUE;
  149. else
  150. return FALSE;
  151. }
  152. }