/ Published in: PHP
The one and only class for all your file handling needs.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php /** * @name FileSystemService.php * @author Jonnie Spratley * @version 1.9 * */ class FileSystemService { private $filename; private $filepath; public function __construct() { } /** * @return unknown */ public function getFilename() { return $this->filename; } /** * @param unknown_type $filename */ public function setFilename( $filename ) { $this->filename = $filename; } /** * @return unknown */ public function getFilepath() { return $this->filepath; } /** * @param unknown_type $filepath */ public function setFilepath( $filepath ) { $this->filepath = $filepath; } /** * I browse the directory * * @param [string] $aPath Path of which directory * @return [array] */ public function browseDirectory( $path = '.', $level = 0, $jsonEncode ) { // Directories to ignore when listing output. Many hosts will deny PHP access to the cgi-bin. 'cgi-bin', '.', '..', '.DS_Store', '.svn' ); // Open the directory to the handle $dh // Loop through the directory { //get the extension from the filename // Check that this file is not to be ignored { // Its a directory, so we need to keep reading down... { //$pathInfo = pathinfo( $file ); // Re-call this same function but on a new directory.this is what makes function recursive. 'filename' => $file, 'path' => "$path/$file", //'filePath' => , 'ext' => $ext, 'children' => $this->browseDirectory ( "$path/$file", $level + 1 ) ); } else { 'filename' => $file, 'path' => "$path/$file", //'filePath' => pathinfo( $file ), 'ext' => $ext, ); # sort ( $folders ); } } } return $folders; } public function browseSubDirectory( $folder ) { { { if ( $file != '.' && $file != '..' ) { 'label' => $file ); } } } return $subdirArray; } /** * I get the disk information * * @param [string] $aPath the path * @return [array] */ public function getDiskInfo( $aPath ) { ); return $diskInfoArray; } /** * I change the permissions on a file, or directory * * @param [string] $whatDir the directory * @param [int] $aPermissions the permissions * @return [boolean] */ public function changePermissions( $whatDir, $aPermissions ) { if ( ! $aPermissions ) { $aPermissions = 0777; } $change = false; // Everything for owner, read and execute for others { $change = true; } return $change; } /** * I write to a file * * @param [string] $filename the file name * @param [string] $contents file contents */ static public function writeFile( $filename, $contents ) { self::recursiveChmod($filename, 0777, 0777); //make sure file is writable //chmod ( $filename ); 'file' => $filename ); if ( $written ) { return $result; } return $filename; } static public function appendFile( $filename, $contents ) { //make sure file is writable //chmod ( $filename ); 'file' => $filename ); if ( $written ) { return $result; } return $filename; } { $data = ''; //$contents = array (); { //make sure file is readable //chmod ( $filename, 0755 ); if ( ! $fp ) { echo 'File could not be opened'; } { //$contents = array ( 'contents' => fgets ( $fp, 9999 )); //dump the contents into an array } } return $data; } /** * I create a directory * * @param [string] $aFolder name of the folder * @param [int] $aPermissions the permissions */ public function createDirectory( $aFolder, $aPermissions = 0755 ) { $message = ''; if ( ! $newPath ) { $message = 'Error creating ' . $newPath; } else { $message = 'Created' . $newPath; } return $message; } /** * I remove a file * * @param [string] $whatDir * @param [string] $whatFile * @return [string] */ public function removeFile( $whatDir, $whatFile ) { $filepath = "$whatDir/$whatFile"; $this->changePermissions ( $filepath, 0777 ); $message = ''; if ( ! $removeFile ) { $message = 'There was a problem removing the file.'; } else { $message = "$whatFile was removed."; } return $message; } public function escapeContents( $contents ) { } private function getFormattedDate( $aFile ) { return $newDate; } public function getDirectory( $path = '.', $level = 0 ) { // Directories to ignore when listing output. Many hosts will deny PHP access to the cgi-bin. 'cgi-bin', '.', '..' ); // Open the directory to the handle $dh // Loop through the directory { // Check that this file is not to be ignored { // Its a directory, so we need to keep reading down... { // Re-call this same function but on a new directory.this is what makes function recursive. 'label' => $file, 'path' => "$path/$file", 'children' => $this->getDirectory ( "$path/$file", $level + 1 ) ); } else { 'label' => $file ); } } } return $folders; } public function compressFile( $filename ) { $zipfile = "$filename.zip"; //Load file //compress file return $zipfile; } /** * I recursivly copy files/folders/contents to a destination. * * @param [string] $dirsource - The source * @param [string] $dirdest - The destination * @return true */ static public function recursive_copy( $dirsource, $dirdest ) { { } { if ( $file != "." && $file != ".." ) { else { $dirdest1 = $dirdest . "/" . $dirname; self::recursive_copy ( $dirsource . "/" . $file, $dirdest1 ); } } } return true; } /** * I recursively move all files in a source to a destination * * @param [string] $dirsource - The source to move * @param [string] $dirdest - The destination where new files will be moved */ static public function recursive_move( $dirsource, $dirdest ) { { } { if ( $file != "." && $file != ".." ) { { } else { $dirdest1 = $dirdest . "/" . $dirname; self::recursive_move ( $dirsource . "/" . $file, $dirdest1 ); } } } } /** Chmods files and folders with different permissions. This is an all-PHP alternative to using: \n @param $path An either relative or absolute path to a file or directory which should be processed. @param $filePerm The permissions any found files should get. @param $dirPerm The permissions any found folder should get. @return Returns TRUE if the path if found and FALSE if not. @warning The permission levels has to be entered in octal format, which normally means adding a zero ("0") in front of the permission level. \n More info at: http://php.net/chmod. */ static public function recursiveChmod($path, $filePerm=0755, $dirPerm=0755) { // Check if the path exists { return(FALSE); } // See whether this is a file { // Chmod the file with our given filepermissions // If this is a directory... // Then get an array of the contents // Remove "." and ".." from the list // Parse every result... foreach($entries as $entry) { // And call this function again recursively, with the same permissions self::recursiveChmod($path."/".$entry, $filePerm, $dirPerm); } // When we are done with the contents of the directory, we chmod the directory itself } // Everything seemed to work out well, return TRUE return(TRUE); } } ?>