Super File System Service


/ Published in: PHP
Save to your folder(s)

The one and only class for all your file handling needs.


Copy this code and paste it in your HTML
  1. <?php
  2. /**
  3.  * @name FileSystemService.php
  4.  * @author Jonnie Spratley
  5.  * @version 1.9
  6.  *
  7.  */
  8.  
  9. class FileSystemService
  10. {
  11. private $filename;
  12. private $filepath;
  13.  
  14. public function __construct()
  15. {
  16. error_reporting ( E_ERROR | E_USER_ERROR | E_PARSE );
  17. }
  18.  
  19. /**
  20. * @return unknown
  21. */
  22. public function getFilename()
  23. {
  24. return $this->filename;
  25. }
  26.  
  27. /**
  28. * @param unknown_type $filename
  29. */
  30. public function setFilename( $filename )
  31. {
  32. $this->filename = $filename;
  33. }
  34.  
  35. /**
  36. * @return unknown
  37. */
  38. public function getFilepath()
  39. {
  40. return $this->filepath;
  41. }
  42.  
  43. /**
  44. * @param unknown_type $filepath
  45. */
  46. public function setFilepath( $filepath )
  47. {
  48. $this->filepath = $filepath;
  49. }
  50.  
  51. /**
  52. * I browse the directory
  53. *
  54. * @param [string] $aPath Path of which directory
  55. * @return [array]
  56. */
  57. public function browseDirectory( $path = '.', $level = 0, $jsonEncode )
  58. {
  59. // Directories to ignore when listing output. Many hosts will deny PHP access to the cgi-bin.
  60. $ignore = array (
  61. 'cgi-bin', '.', '..', '.DS_Store', '.svn'
  62. );
  63.  
  64. // Open the directory to the handle $dh
  65. $dh = opendir ( $path );
  66.  
  67. $folders = array ();
  68.  
  69. // Loop through the directory
  70. while ( false !== ( $file = readdir ( $dh ) ) )
  71. {
  72. //get the extension from the filename
  73. $ext = substr ( strrchr ( $file, '.' ), 1 );
  74.  
  75. // Check that this file is not to be ignored
  76. if ( ! in_array ( $file, $ignore ) )
  77. {
  78.  
  79. // Its a directory, so we need to keep reading down...
  80. if ( is_dir ( "$path/$file" ) )
  81. {
  82. //$pathInfo = pathinfo( $file );
  83.  
  84.  
  85. // Re-call this same function but on a new directory.this is what makes function recursive.
  86. $folders [] = array (
  87.  
  88. 'filename' => $file,
  89. 'path' => "$path/$file",
  90. //'filePath' => ,
  91. 'ext' => $ext,
  92. 'isDir' => ( is_dir ( $file ) ) ? 'true' : 'false',
  93. 'isFile' => ( is_file ( $file ) ) ? 'true' : 'false',
  94. 'isWritable' => ( is_writable ( $file ) ) ? 'true' : 'false',
  95. 'isReadable' => ( is_readable ( $file ) ) ? 'true' : 'false',
  96. 'children' => $this->browseDirectory ( "$path/$file", $level + 1 )
  97. );
  98.  
  99. rsort ( $folders );
  100. }
  101. else
  102. {
  103.  
  104. $folders [] = array (
  105.  
  106. 'filename' => $file,
  107. 'path' => "$path/$file",
  108. //'filePath' => pathinfo( $file ),
  109. 'ext' => $ext,
  110. 'isDir' => ( is_dir ( $file ) ) ? 'true' : 'false',
  111. 'isFile' => ( is_file ( $file ) ) ? 'true' : 'false',
  112. 'isWritable' => ( is_writable ( $file ) ) ? 'true' : 'false',
  113. 'isReadable' => ( is_readable ( $file ) ) ? 'true' : 'false'
  114. );
  115. # sort ( $folders );
  116. }
  117. }
  118. }
  119.  
  120. closedir ( $dh );
  121.  
  122. return $folders;
  123. }
  124.  
  125. public function browseSubDirectory( $folder )
  126. {
  127. $subdirArray = array ();
  128. $subDirHandle = opendir ( $folder );
  129. {
  130. while ( false !== ( $file = readdir ( $subDirHandle ) ) )
  131. {
  132. if ( $file != '.' && $file != '..' )
  133. {
  134. $subdirArray [] = array (
  135. 'label' => $file
  136. );
  137. }
  138.  
  139. }
  140. closedir ( $subDirHandle );
  141. }
  142. return $subdirArray;
  143. }
  144.  
  145. /**
  146. * I get the disk information
  147. *
  148. * @param [string] $aPath the path
  149. * @return [array]
  150. */
  151. public function getDiskInfo( $aPath )
  152. {
  153. $diskInfoArray = array (
  154. 'freeSpace' => disk_free_space ( $aPath ), 'totalSize' => disk_total_space ( $aPath )
  155. );
  156.  
  157. return $diskInfoArray;
  158. }
  159.  
  160. /**
  161. * I change the permissions on a file, or directory
  162. *
  163. * @param [string] $whatDir the directory
  164. * @param [int] $aPermissions the permissions
  165. * @return [boolean]
  166. */
  167. public function changePermissions( $whatDir, $aPermissions )
  168. {
  169.  
  170. if ( ! $aPermissions )
  171. {
  172. $aPermissions = 0777;
  173. }
  174. $change = false;
  175. // Everything for owner, read and execute for others
  176.  
  177. if ( chmod ( $whatDir, $aPermissions ) )
  178. {
  179. $change = true;
  180. }
  181.  
  182. return $change;
  183. }
  184.  
  185. /**
  186. * I write to a file
  187. *
  188. * @param [string] $filename the file name
  189. * @param [string] $contents file contents
  190. */
  191. static public function writeFile( $filename, $contents )
  192. {
  193. self::recursiveChmod($filename, 0777, 0777);
  194. $fp = fopen ( $filename, 'w' );
  195.  
  196. //make sure file is writable
  197. //chmod ( $filename );
  198. $result = array (
  199. 'file' => $filename
  200. );
  201.  
  202. $written = fwrite ( $fp, $contents );
  203. if ( $written )
  204. {
  205. return $result;
  206. }
  207.  
  208. fclose ( $fp );
  209.  
  210. return $filename;
  211. }
  212.  
  213. static public function appendFile( $filename, $contents )
  214. {
  215.  
  216. $fp = fopen ( $filename, 'a' );
  217. //make sure file is writable
  218. //chmod ( $filename );
  219. $result = array (
  220. 'file' => $filename
  221. );
  222.  
  223. $written = fwrite ( $fp, $contents."\n" );
  224. if ( $written )
  225. {
  226. return $result;
  227. }
  228.  
  229. fclose ( $fp );
  230.  
  231. return $filename;
  232. }
  233.  
  234. static public function readFile( $file )
  235. {
  236. $data = '';
  237. //$contents = array ();
  238. if ( is_file ( $file ) )
  239. {
  240.  
  241. $fp = fopen ( $file, 'r' );
  242.  
  243. //make sure file is readable
  244. //chmod ( $filename, 0755 );
  245.  
  246.  
  247. if ( ! $fp )
  248. {
  249. echo 'File could not be opened';
  250. exit ();
  251. }
  252.  
  253. while ( ! feof ( $fp ) )
  254. {
  255. //$contents = array ( 'contents' => fgets ( $fp, 9999 ));
  256. $data .= fgets ( $fp, 9999 );
  257. //dump the contents into an array
  258. }
  259.  
  260. fclose ( $fp );
  261. }
  262. return $data;
  263. }
  264.  
  265. /**
  266. * I create a directory
  267. *
  268. * @param [string] $aFolder name of the folder
  269. * @param [int] $aPermissions the permissions
  270. */
  271. public function createDirectory( $aFolder, $aPermissions = 0755 )
  272. {
  273. $oldmask = umask ( 0 );
  274. $newPath = mkdir ( $aFolder, $aPermissions );
  275. $message = '';
  276.  
  277. umask ( $oldmask );
  278.  
  279. if ( ! $newPath )
  280. {
  281. $message = 'Error creating ' . $newPath;
  282. }
  283. else
  284. {
  285. $message = 'Created' . $newPath;
  286. }
  287. return $message;
  288. }
  289.  
  290. /**
  291. * I remove a file
  292. *
  293. * @param [string] $whatDir
  294. * @param [string] $whatFile
  295. * @return [string]
  296. */
  297. public function removeFile( $whatDir, $whatFile )
  298. {
  299. $filepath = "$whatDir/$whatFile";
  300. $this->changePermissions ( $filepath, 0777 );
  301.  
  302. $removeFile = unlink ( $filepath );
  303. $message = '';
  304. if ( ! $removeFile )
  305. {
  306. $message = 'There was a problem removing the file.';
  307. }
  308. else
  309. {
  310. $message = "$whatFile was removed.";
  311. }
  312. return $message;
  313. }
  314.  
  315. public function escapeContents( $contents )
  316. {
  317. return htmlspecialchars ( $contents );
  318. }
  319.  
  320. private function getFormattedDate( $aFile )
  321. {
  322. $newDate = date ( 'j F Y H:i', $aFile );
  323.  
  324. return $newDate;
  325. }
  326.  
  327. public function getDirectory( $path = '.', $level = 0 )
  328. {
  329. // Directories to ignore when listing output. Many hosts will deny PHP access to the cgi-bin.
  330. $ignore = array (
  331. 'cgi-bin', '.', '..'
  332. );
  333.  
  334. // Open the directory to the handle $dh
  335. $dh = opendir ( $path );
  336.  
  337. $folders = array ();
  338. $subFolders = array ();
  339.  
  340. // Loop through the directory
  341. while ( false !== ( $file = readdir ( $dh ) ) )
  342. {
  343. // Check that this file is not to be ignored
  344. if ( ! in_array ( $file, $ignore ) )
  345. {
  346. // Its a directory, so we need to keep reading down...
  347. if ( is_dir ( "$path/$file" ) )
  348. {
  349. // Re-call this same function but on a new directory.this is what makes function recursive.
  350. $folders [] = array (
  351. 'label' => $file, 'path' => "$path/$file", 'children' => $this->getDirectory ( "$path/$file", $level + 1 )
  352. );
  353. }
  354. else
  355. {
  356. $folders [] = array (
  357. 'label' => $file
  358. );
  359. }
  360. }
  361. }
  362. closedir ( $dh );
  363. return $folders;
  364. }
  365.  
  366. public function compressFile( $filename )
  367. {
  368. $zipfile = "$filename.zip";
  369.  
  370. //Load file
  371. $data = file_get_contents ( $filename );
  372.  
  373. //compress file
  374. file_put_contents ( "compress.zlib://$zipfile", $data );
  375.  
  376. return $zipfile;
  377. }
  378.  
  379. /**
  380. * I recursivly copy files/folders/contents to a destination.
  381. *
  382. * @param [string] $dirsource - The source
  383. * @param [string] $dirdest - The destination
  384. * @return true
  385. */
  386. static public function recursive_copy( $dirsource, $dirdest )
  387. {
  388. if ( is_dir ( $dirsource ) )
  389. {
  390. $dir_handle = opendir ( $dirsource );
  391. }
  392. $dirname = substr ( $dirsource, strrpos ( $dirsource, "/" ) + 1 );
  393.  
  394. mkdir ( $dirdest . "/" . $dirname, 0777 );
  395.  
  396. while ( $file = readdir ( $dir_handle ) )
  397. {
  398. if ( $file != "." && $file != ".." )
  399. {
  400. if ( ! is_dir ( $dirsource . "/" . $file ) ) copy ( $dirsource . "/" . $file, $dirdest . "/" . $dirname . "/" . $file );
  401. else
  402. {
  403. $dirdest1 = $dirdest . "/" . $dirname;
  404. self::recursive_copy ( $dirsource . "/" . $file, $dirdest1 );
  405. }
  406. }
  407. }
  408. closedir ( $dir_handle );
  409.  
  410. return true;
  411. }
  412.  
  413. /**
  414. * I recursively move all files in a source to a destination
  415. *
  416. * @param [string] $dirsource - The source to move
  417. * @param [string] $dirdest - The destination where new files will be moved
  418. */
  419. static public function recursive_move( $dirsource, $dirdest )
  420. {
  421. if ( is_dir ( $dirsource ) )
  422. {
  423. $dir_handle = opendir ( $dirsource );
  424. }
  425.  
  426. $dirname = substr ( $dirsource, strrpos ( $dirsource, "/" ) + 1 );
  427.  
  428. mkdir ( $dirdest . "/" . $dirname, 0777 );
  429.  
  430. while ( $file = readdir ( $dir_handle ) )
  431. {
  432. if ( $file != "." && $file != ".." )
  433. {
  434. if ( ! is_dir ( $dirsource . "/" . $file ) )
  435. {
  436. copy ( $dirsource . "/" . $file, $dirdest . "/" . $dirname . "/" . $file );
  437. unlink ( $dirsource . "/" . $file );
  438. }
  439. else
  440. {
  441. $dirdest1 = $dirdest . "/" . $dirname;
  442. self::recursive_move ( $dirsource . "/" . $file, $dirdest1 );
  443. }
  444. }
  445. }
  446. closedir ( $dir_handle );
  447. rmdir ( $dirsource );
  448. }
  449. /**
  450.   Chmods files and folders with different permissions.
  451.  
  452.   This is an all-PHP alternative to using: \n
  453.  
  454.   @param $path An either relative or absolute path to a file or directory
  455.   which should be processed.
  456.   @param $filePerm The permissions any found files should get.
  457.   @param $dirPerm The permissions any found folder should get.
  458.   @return Returns TRUE if the path if found and FALSE if not.
  459.   @warning The permission levels has to be entered in octal format, which
  460.   normally means adding a zero ("0") in front of the permission level. \n
  461.   More info at: http://php.net/chmod.
  462.   */
  463.  
  464. static public function recursiveChmod($path, $filePerm=0755, $dirPerm=0755)
  465. {
  466. // Check if the path exists
  467. if(!file_exists($path))
  468. {
  469. return(FALSE);
  470. }
  471. // See whether this is a file
  472. if(is_file($path))
  473. {
  474. // Chmod the file with our given filepermissions
  475. chmod($path, $filePerm);
  476. // If this is a directory...
  477. } elseif(is_dir($path)) {
  478. // Then get an array of the contents
  479. $foldersAndFiles = scandir($path);
  480. // Remove "." and ".." from the list
  481. $entries = array_slice($foldersAndFiles, 2);
  482. // Parse every result...
  483. foreach($entries as $entry)
  484. {
  485. // And call this function again recursively, with the same permissions
  486. self::recursiveChmod($path."/".$entry, $filePerm, $dirPerm);
  487. }
  488. // When we are done with the contents of the directory, we chmod the directory itself
  489. chmod($path, $dirPerm);
  490. }
  491. // Everything seemed to work out well, return TRUE
  492. return(TRUE);
  493. }
  494.  
  495.  
  496.  
  497. }
  498.  
  499. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.