OpenSencillo  2016.106
Long live the simplicity of PHP
 All Data Structures Namespaces Files Functions Pages
files.management.fman.php
1 <?php
11 class filesList
12 {
13  private $files;
14  private $out;
15  protected $path;
16  protected $folderlist;
17 
18  public function __construct($path)
19  {
20  $this->files = scandir($path);
21  $this->path = $path;
22 
23  if (!file_exists($path))
24  {
25  mkdir($path, 0777, true);
26  chmod($path,0777);
27  }
28  }
36  public function scanDir($dir='./')
37  {
38  $this->folderlist = array();
39  $this->findFiles($dir, $this->folderlist);
40  return $this->folderlist;
41  }
48  public function findFiles($dir, &$dir_array)
49  {
50  // Create array of current directory
51  $files = scandir($dir);
52 
53  if(is_array($files))
54  {
55  foreach($files as $val)
56  {
57  // Skip home and previous listings
58  if($val == '.' || $val == '..')
59  continue;
60 
61  // If directory then dive deeper, else add file to directory key
62  if(is_dir($dir.'/'.$val))
63  {
64  // Add value to current array, dir or file
65  $dir_array[$dir][] = $val;
66 
67  findFiles($dir.'/'.$val, $dir_array);
68  }
69  else
70  {
71  $dir_array[$dir][] = $val;
72  }
73  }
74  }
75  ksort($dir_array);
76  }
77 
85  public function findFilesStructure($dir)
86  {
87  $result = array();
88 
89  $cdir = scandir($dir);
90  foreach ($cdir as $key => $value)
91  {
92  if (!in_array($value,array(".","..")))
93  {
94  if (is_dir($dir . DIRECTORY_SEPARATOR . $value))
95  {
96  $result[$value] = dirToArray($dir . DIRECTORY_SEPARATOR . $value);
97  }
98  else
99  {
100  $result[] = $value;
101  }
102  }
103  }
104 
105  return $result;
106  }
115  public function arrayToHtml($style,$folderStyle=null,$ftypes=null,$dot=null)
116  {
117  $i=0;
118  $this->out='';
119 
120  while(sizeof($this->files)>$i)
121  {
122  if(($dot==false)||($dot==null))
123  {
124  if(stristr($this->files[$i],$ftypes)!=false)
125  {
126  //is file
127  if(($this->files[$i]!='.')&&($this->files[$i]!='..'))
128  {
129  $this->out.=$style[0].$this->files[$i++].$style[1];
130  }
131  else
132  {
133  $i++;
134  }
135  }
136  else
137  {
138  //is folder
139  if(($this->files[$i]!='.')&&($this->files[$i]!='..'))
140  {
141  $this->out.=$folderStyle[0].$this->files[$i++].$folderStyle[1];
142  }
143  else
144  {
145  $i++;
146  }
147  }
148  }
149  else
150  {
151  if(is_file($this->files[$i]))
152  {
153  $this->out.=$style[0].$this->files[$i++].$style[1];
154  }
155  else
156  {
157  $this->out.=$folderStyle[0].$this->files[$i++].$folderStyle[1];
158  }
159  }
160  }
161  return $this->out;
162  }
163 }
164 ?>