/files/files.class.php

https://github.com/oshanrube/php-Filemanager · PHP · 175 lines · 136 code · 7 blank · 32 comment · 28 complexity · 0332b721cefce6f13e770cadc3f7ad34 MD5 · raw file

  1. <?php
  2. class Files{
  3. var $tobedeleted = array();
  4. var $files = array();
  5. var $folders = array();
  6. public function __construct() {
  7. }
  8. //getFile from inode
  9. public function getFile($inode) {
  10. foreach($this->files as $file){
  11. if($file->inode == $inode){
  12. return $file;
  13. }
  14. }
  15. }
  16. //get parent
  17. public function getParent($file) {
  18. return dechex(fileinode($file->path));
  19. }
  20. //get the folders only
  21. public function getFolders($path = '/') {
  22. foreach($this->files as $file){
  23. if($file->isDir && $file->folder == $path){
  24. $this->folders[] = $file;
  25. }
  26. }
  27. sort($this->folders);
  28. return $this->folders;
  29. }
  30. //get the files only
  31. public function getFiles($path = '/') {
  32. foreach($this->files as $file){
  33. if(!$file->isDir && $file->folder == $path){
  34. $this->filesOnly[] = $file;
  35. }
  36. }
  37. sort($this->filesOnly);
  38. return $this->filesOnly;
  39. }
  40. //dump files
  41. public function dumpFiles() {
  42. return $this->files;
  43. }
  44. //load files
  45. public function loadFiles($files) {
  46. $this->files = $files;
  47. }
  48. //list the files
  49. function items($BASEDIR,$path, $filter, $recurse, $full, $exclude, $excludefilter,$deletefilter,$depth = 1) {
  50. // Compute the excludefilter string
  51. if(count($excludefilter))
  52. $excludefilter_string = '/('. implode('|', $excludefilter) .')/';
  53. else
  54. $excludefilter_string = '';
  55. // Compute the excludefilter string
  56. if(count($deletefilter))
  57. $deletefilter_string = '/('. implode('|', $deletefilter) .')/';
  58. else
  59. $deletefilter_string = '';
  60. // Initialise variables.
  61. $arr = array();
  62. if(!is_dir($BASEDIR.$path)) die('path does not exsist:'.$BASEDIR.$path);
  63. // read the source directory
  64. $handle = opendir($BASEDIR.$path);
  65. while (($file = readdir($handle)) !== false)
  66. {
  67. if ($file != '.' && $file != '..' && !in_array($file, $exclude) && (empty($excludefilter_string) || !preg_match($excludefilter_string, $file)))
  68. {
  69. // Compute the fullpath and absolutepath
  70. $fullpath = $path .'/'. $file;
  71. $absolutePath = $BASEDIR.$fullpath;
  72. // Compute the isDir flag
  73. $isDir = is_dir($absolutePath);
  74. //check wether it matches delete patern
  75. if(preg_match($deletefilter_string, $file)) {
  76. //addto tobedeleted
  77. $this->tobedeleted[] = $fullpath;
  78. }else {
  79. $arrfile = null;
  80. // store file details
  81. $arrfile->filename = $file;
  82. $arrfile->path = dirname($absolutePath);
  83. $arrfile->folder = dirname($fullpath);
  84. $arrfile->isDir = $isDir;
  85. $arrfile->filesize = dechex(filesize($absolutePath));
  86. $arrfile->filemtime = filemtime($absolutePath);
  87. $arrfile->inode = dechex(fileinode($absolutePath));
  88. $arr[] = $arrfile;
  89. }
  90. //if directory
  91. if ($isDir && $recurse)
  92. {
  93. // Search recursively
  94. if ($recurse){
  95. $arr = array_merge($arr, $this->items($BASEDIR,$fullpath, $filter, $recurse, $full, $exclude, $excludefilter,$deletefilter,$depth+1));
  96. }
  97. }
  98. }
  99. }
  100. closedir($handle);
  101. $this->files = $arr;
  102. return $arr;
  103. }
  104. function getFilesToBeDeleted() {
  105. return $this->tobedeleted;
  106. }
  107. // Sort the files
  108. // asort($arr);
  109. // var_dump( file('.htaccess'));
  110. //$filename = '.htaccess';
  111. //echo "$filename was last changed: " . date("F d Y H:i:s.", filemtime($filename))."\n";
  112. //echo hexdec(dechex(filesize($filename)));
  113. //echo var_dump( $arr);
  114. //var_dump( array_values($tobedeleted));
  115. //---------------------------------------------------------------------------------
  116. function writeToFile($arr) {
  117. //lines per file
  118. $chunk = 5000;
  119. //initialize the filename
  120. $filename = 'filedata-1.txt';
  121. if(count($arr) > $chunk) {
  122. $files = array_chunk($arr,$chunk,true);
  123. } else {
  124. $files[] = $arr;
  125. }
  126. foreach($files as $file){
  127. while(file_exists($filename)) {
  128. //if the file is already there create a new one
  129. $filename = 'filedata-'.(preg_replace('/.*-([0-9]+)\.txt/','$1',$filename)+1).'.txt';
  130. }
  131. if (!$handle = fopen($filename, 'a+')) {
  132. echo "Cannot open file ($filename)";
  133. exit;
  134. }
  135. foreach($file as $item){
  136. $string = serialize($item)."\n";
  137. if (fwrite($handle, $string) === FALSE) {
  138. echo "Cannot write to file ($filename)";
  139. exit;
  140. }
  141. }
  142. fclose($handle);
  143. }
  144. }
  145. //---------------------------------------------------------------------------------------
  146. //initialize the array to store the file objects
  147. function readFromFile() {
  148. $arr = array();
  149. $filename = 'filedata-1.txt';
  150. while(file_exists($filename)) {
  151. $chunksize = 1 * (1024 * 1); // how many bytes per chunk
  152. $handle = fopen($filename, 'rb');
  153. $buffer = '';
  154. while (($buffer = fgets($handle, $chunksize)) !== false) {
  155. $arr[] = $buffer;
  156. ob_flush();
  157. flush();
  158. }
  159. fclose($handle);
  160. $filename = 'filedata-'.(preg_replace('/.*-([0-9]+)\.txt/','$1',$filename)+1).'.txt';
  161. }
  162. return $arr;
  163. }
  164. public static function makesafe($file) {
  165. return $file;
  166. }
  167. }
  168. ?>