/ Published in: PHP
This was more of a go at learning the basics of OOP, however I decided to document it well in case anyone had any comments. Note: It only works with single files, no directory usage.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php class fileManipulation { protected $__workingFile = NULL; protected $_workingHandle = NULL; protected $_workingMode = NULL; protected $_handleExists = FALSE; function __construct($fileName) { // Error out if the file is a directory, or doesn't exist { } else { $this->_workingFile = $fileName; } } /* ####################### FILE INFORMATION ####################### */ /** * * @return string Name of file that the user is working with. */ function getFile() { return $this->_workingFile; } /** * Allows the end user to retrieve specific information about the working file. * * @return array Associative array of specific file information. */ function information() { // Raw filesize followed by MB (Hundreths placement) // Use stat() to retrieve basic access info $lastAccess = $statInfo['atime']; $lastModified = $statInfo['mtime']; // Retrieve complex permission values // Retrieve last 3 complex file permission values (ie 777 or 644) // Convert complex permissions to rw-r.. format $ufPermissions = $this->_advancedPerms($complexPermissions); // Simple file permissions ## -- Note: is_writable() handles errors with an E_WARNING, so set value to 0(False) upon failure // Return associative array to obtain singled out information "fileSize" => $fileSizeMB, "lastAccess" => $lastAccess, "lastModified" => $lastModified, "complexPerms" => $complexPermissions, "simplePerms" => $simplePermissions, "ufPerms" => $ufPermissions, "isReadable" => $isReadable, "isWritable" => $isWritable); } /** * Takes a complex permission string and turns it into a symbolic * notation, protected since it's only available to the information() function. * * @param int $permString A string of permissions in the format of: 33206 * @return string A string in the format of -rw-r--r-- */ protected function _advancedPerms($permString) { if (($permString & 0xC000) == 0xC000) { $notation = "s"; // Socket } elseif (($permString & 0xA000) == 0xA000) { $notation = "l"; // Symbolic Link } elseif (($permString & 0x8000) == 0x8000) { $notation = "-"; // Regular } elseif (($permString & 0x6000) == 0x6000) { $notation = "b"; // Block special } elseif (($permString & 0x4000) == 0x4000) { $notation = "d"; // Directory } elseif (($permString & 0x2000) == 0x2000) { $notation = "c"; // Character special } elseif (($permString & 0x1000) == 0x1000) { $notation = "p"; // FIFO pipe } else { $notation = "u"; // Unknown } // Owner $notation .= (($permString & 0x0100) ? 'r' : '-'); $notation .= (($permString & 0x0080) ? 'w' : '-'); $notation .= (($permString & 0x0040) ? (($permString & 0x0800) ? 's' : 'x' ) : (($permString & 0x0800) ? 'S' : '-')); // Group $notation .= (($permString & 0x0020) ? 'r' : '-'); $notation .= (($permString & 0x0010) ? 'w' : '-'); $notation .= (($permString & 0x0008) ? (($permString & 0x0400) ? 's' : 'x' ) : (($permString & 0x0400) ? 'S' : '-')); // World $notation .= (($permString & 0x0004) ? 'r' : '-'); $notation .= (($permString & 0x0002) ? 'w' : '-'); $notation .= (($permString & 0x0001) ? (($permString & 0x0200) ? 't' : 'x' ) : (($permString & 0x0200) ? 'T' : '-')); return $notation; } /* ####################### FILE INFORMATION ####################### */ /* ####################### FILE MODIFICATIONS ####################### */ function newMode($newMode) { // decoct makes 0777, 777, etc } function hardCopy($newName) // hardCopy overwrites files that exist { } function softCopy($newName) // if file exists dont copy, otherwise copy { } function newName($newName) { } function remove() { } /* ####################### FILE MODIFICATIONS ####################### */ /** * Allows for a manual method of setting the fopen handle, with an optional * parameter of setting the write mode to something other than a+. * * @param string $writeMode What mode the user wants to open the file with. * @return NONE */ function setHandle($writeMode="a+") { $this->_workingMode = $writeMode; $this->_handleExists = ($this->_workingHandle) ? TRUE : FALSE; return; } /** * * @param string $preText Text to display before the reading of the file. * @return string Contents of file being read with fread() */ function read($preText=NULL) { if (!$this->_handleExists) { $this->setHandle(); } } /** * * @param string/array $data Whatever information desired to be written to the working file. * @return NONE */ function write($data) { if ($this->_workingMode=="r") { die("Invalid mode for writing. <br /><strong>Mode Specified: \"".$this->_workingMode."\"</strong>"); } if (!$this->_handleExists) { $this->setHandle(); } return; } /** * Read a file out into an array without requiring a handle such as fopen, * it uses the file function. * * @param int $limit Limit the amount of lines you want to be returned in the array * @return array An array of elements including each individual line */ function lineByLine($limit=0) { // Setup the array of each line, and a count of total lines // If the user set a limit on how many lines they want returned if ($limit!=0) { // Loop through and unset the last element of the array // until it is to the desired line limit for ($i=$lineCount; $i>$limit; $i--) { } } return $arrayOfLines; } function __destruct() { // If any handles exist, ie - a file was opened // and not closed, we close it now if ($this->_handleExists) { } } }