File System Service


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



Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. class FileSystemService
  4. {
  5. private $filename;
  6. private $filepath;
  7.  
  8. function __construct()
  9. {
  10. error_reporting ( E_ERROR | E_USER_ERROR | E_PARSE );
  11. }
  12.  
  13. /**
  14. * @return unknown
  15. */
  16. public function getFilename()
  17. {
  18. return $this->filename;
  19. }
  20.  
  21. /**
  22. * @param unknown_type $filename
  23. */
  24. public function setFilename( $filename )
  25. {
  26. $this->filename = $filename;
  27. }
  28.  
  29. /**
  30. * @return unknown
  31. */
  32. public function getFilepath()
  33. {
  34. return $this->filepath;
  35. }
  36.  
  37. /**
  38. * @param unknown_type $filepath
  39. */
  40. public function setFilepath( $filepath )
  41. {
  42. $this->filepath = $filepath;
  43. }
  44.  
  45. public function browseDirectory( $aPath, $aResultFormat = '' )
  46. {
  47. $directoryHandle = opendir ( $aPath );
  48.  
  49. while ( $file = readdir ( $directoryHandle ) )
  50. {
  51. //loop files
  52. $fileInfoArray[] = array(
  53. 'aFilename' => $file, 'aSize' => filesize ( $file ), 'aCreated' => filemtime ( $file ), 'aType' => filetype ( $file ), 'aDatabase' => '', 'aPermissions' => decoct ( fileperms ( $file ) ), 'isDir' => ( is_dir ( $file ) ) ? 'true' : 'false', 'isFile' => ( is_file ( $file ) ) ? 'true' : 'false', 'isReadable' => ( is_readable ( $file ) ) ? 'true' : 'false', 'isWritable' => ( is_writable ( $file ) ) ? 'true' : 'false'
  54. );
  55. }
  56. closedir ( $directoryHandle );
  57.  
  58. return json_encode ( $fileInfoArray );
  59. }
  60.  
  61. public function getDiskInfo( $aPath )
  62. {
  63. //disk information
  64. $diskInfoArray = array(
  65. 'diskFreeSpace' => disk_free_space ( $aPath ), 'diskTotalSize' => disk_total_space ( $aPath )
  66. );
  67.  
  68. return $diskInfoArray;
  69. }
  70.  
  71. public function changePermissions( $whatDir, $aPermissions = 0755 )
  72. {
  73. // Everything for owner, read and execute for others
  74. $change = chmod ( $whatDir, $aPermissions );
  75. if ( $change )
  76. {
  77. return true;
  78. }
  79.  
  80. }
  81.  
  82. public function createDirectory( $aFolder, $aPermissions = 0777 )
  83. {
  84. $oldmask = umask ( 0 );
  85. $newPath = mkdir ( $aFolder, $aPermissions );
  86.  
  87. umask ( $oldmask );
  88. if ( ! $newPath )
  89. {
  90. echo 'Error creating folders';
  91. } else
  92. {
  93.  
  94. echo 'created';
  95. }
  96. }
  97.  
  98. public function removeFile( $whatDir, $whatFile )
  99. {
  100. $filepath = "$whatDir/$whatFile";
  101. $this->changePermissions ( $filepath, 0777 );
  102.  
  103. $removeFile = unlink ( $filepath );
  104.  
  105. if ( $removeFile )
  106. {
  107. return "$whatFile was removed.";
  108. }
  109. return 'There was a problem removing the file.';
  110. }
  111.  
  112. public function getFormattedDate( $aFile )
  113. {
  114. $newDate = date ( 'j F Y H:i', $aFile );
  115.  
  116. return $newDate;
  117. }
  118.  
  119. public function dump_var( $var )
  120. {
  121. print "<pre>\n";
  122. print_r ( $var );
  123. print "</pre>\n";
  124. }
  125. }
  126.  
  127. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.