File Manipulation/Information


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

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.


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. class fileManipulation
  4. {
  5. protected $__workingFile = NULL;
  6. protected $_workingHandle = NULL;
  7. protected $_workingMode = NULL;
  8. protected $_handleExists = FALSE;
  9.  
  10.  
  11. function __construct($fileName)
  12. {
  13. // Error out if the file is a directory, or doesn't exist
  14. if ((!@file_exists($fileName)) || (@is_dir($fileName)))
  15. {
  16. exit("File does not exist, or is a directory.");
  17. } else {
  18. $this->_workingFile = $fileName;
  19. }
  20. }
  21.  
  22. /* ####################### FILE INFORMATION ####################### */
  23. /**
  24.   *
  25.   * @return string Name of file that the user is working with.
  26.   */
  27. function getFile()
  28. {
  29. return $this->_workingFile;
  30. }
  31.  
  32. /**
  33.   * Allows the end user to retrieve specific information about the working file.
  34.   *
  35.   * @return array Associative array of specific file information.
  36.   */
  37. function information()
  38. {
  39. // Raw filesize followed by MB (Hundreths placement)
  40. $fileSize = @filesize($this->_workingFile);
  41. $fileSizeMB = @round($fileSize/1048576, 2);
  42.  
  43. // Use stat() to retrieve basic access info
  44. $statInfo = @stat($this->_workingFile);
  45. $lastAccess = $statInfo['atime'];
  46. $lastModified = $statInfo['mtime'];
  47.  
  48. // Retrieve complex permission values
  49. $complexPermissions = @fileperms($this->_workingFile);
  50. // Retrieve last 3 complex file permission values (ie 777 or 644)
  51. $simplePermissions = @substr(decoct($complexPermissions), 3);
  52. // Convert complex permissions to rw-r.. format
  53. $ufPermissions = $this->_advancedPerms($complexPermissions);
  54.  
  55. // Simple file permissions
  56. $isReadable = @is_readable($this->_workingFile);
  57. ## -- Note: is_writable() handles errors with an E_WARNING, so set value to 0(False) upon failure
  58. $isWritable = (@is_writable($this->_workingFile)!=1) ? 0 : 1;
  59.  
  60. // Return associative array to obtain singled out information
  61. return array(
  62. "fileSize" => $fileSizeMB,
  63. "lastAccess" => $lastAccess,
  64. "lastModified" => $lastModified,
  65. "complexPerms" => $complexPermissions,
  66. "simplePerms" => $simplePermissions,
  67. "ufPerms" => $ufPermissions,
  68. "isReadable" => $isReadable,
  69. "isWritable" => $isWritable);
  70. }
  71.  
  72. /**
  73.   * Takes a complex permission string and turns it into a symbolic
  74.   * notation, protected since it's only available to the information() function.
  75.   *
  76.   * @param int $permString A string of permissions in the format of: 33206
  77.   * @return string A string in the format of -rw-r--r--
  78.   */
  79. protected function _advancedPerms($permString)
  80. {
  81. if (($permString & 0xC000) == 0xC000)
  82. {
  83. $notation = "s"; // Socket
  84. } elseif (($permString & 0xA000) == 0xA000)
  85. {
  86. $notation = "l"; // Symbolic Link
  87. } elseif (($permString & 0x8000) == 0x8000)
  88. {
  89. $notation = "-"; // Regular
  90. } elseif (($permString & 0x6000) == 0x6000)
  91. {
  92. $notation = "b"; // Block special
  93. } elseif (($permString & 0x4000) == 0x4000)
  94. {
  95. $notation = "d"; // Directory
  96. } elseif (($permString & 0x2000) == 0x2000)
  97. {
  98. $notation = "c"; // Character special
  99. } elseif (($permString & 0x1000) == 0x1000)
  100. {
  101. $notation = "p"; // FIFO pipe
  102. } else {
  103. $notation = "u"; // Unknown
  104. }
  105.  
  106.  
  107.  
  108. // Owner
  109. $notation .= (($permString & 0x0100) ? 'r' : '-');
  110. $notation .= (($permString & 0x0080) ? 'w' : '-');
  111. $notation .= (($permString & 0x0040) ? (($permString & 0x0800) ? 's' : 'x' ) : (($permString & 0x0800) ? 'S' : '-'));
  112.  
  113. // Group
  114. $notation .= (($permString & 0x0020) ? 'r' : '-');
  115. $notation .= (($permString & 0x0010) ? 'w' : '-');
  116. $notation .= (($permString & 0x0008) ? (($permString & 0x0400) ? 's' : 'x' ) : (($permString & 0x0400) ? 'S' : '-'));
  117.  
  118. // World
  119. $notation .= (($permString & 0x0004) ? 'r' : '-');
  120. $notation .= (($permString & 0x0002) ? 'w' : '-');
  121. $notation .= (($permString & 0x0001) ? (($permString & 0x0200) ? 't' : 'x' ) : (($permString & 0x0200) ? 'T' : '-'));
  122.  
  123. return $notation;
  124. }
  125. /* ####################### FILE INFORMATION ####################### */
  126.  
  127.  
  128. /* ####################### FILE MODIFICATIONS ####################### */
  129. function newMode($newMode)
  130. {
  131. // decoct makes 0777, 777, etc
  132. return (@chmod($this->_workingFile, decoct($newMode)));
  133. }
  134.  
  135. function hardCopy($newName) // hardCopy overwrites files that exist
  136. {
  137. return @copy($this->_workingFile, $newName);
  138. }
  139.  
  140. function softCopy($newName) // if file exists dont copy, otherwise copy
  141. {
  142. return (@file_exists($newName)) ? FALSE : (@copy($this->_workingFile, $newName));
  143. }
  144.  
  145. function newName($newName)
  146. {
  147. return @rename($this->_workingFile, $newName);
  148. }
  149.  
  150. function remove()
  151. {
  152. return unlink($this->_workingFile);
  153. }
  154. /* ####################### FILE MODIFICATIONS ####################### */
  155.  
  156. /**
  157.   * Allows for a manual method of setting the fopen handle, with an optional
  158.   * parameter of setting the write mode to something other than a+.
  159.   *
  160.   * @param string $writeMode What mode the user wants to open the file with.
  161.   * @return NONE
  162.   */
  163. function setHandle($writeMode="a+")
  164. {
  165. $this->_workingHandle = @fopen($this->_workingFile, $writeMode);
  166. $this->_workingMode = $writeMode;
  167. $this->_handleExists = ($this->_workingHandle) ? TRUE : FALSE;
  168. return;
  169. }
  170.  
  171. /**
  172.   *
  173.   * @param string $preText Text to display before the reading of the file.
  174.   * @return string Contents of file being read with fread()
  175.   */
  176. function read($preText=NULL)
  177. {
  178. if (!$this->_handleExists)
  179. {
  180. $this->setHandle();
  181. }
  182.  
  183. return $preText."<br />".fread($this->_workingHandle, filesize($this->_workingFile));
  184. }
  185.  
  186. /**
  187.   *
  188.   * @param string/array $data Whatever information desired to be written to the working file.
  189.   * @return NONE
  190.   */
  191. function write($data)
  192. {
  193. if ($this->_workingMode=="r")
  194. {
  195. die("Invalid mode for writing. <br /><strong>Mode Specified: \"".$this->_workingMode."\"</strong>");
  196. }
  197.  
  198. if (!$this->_handleExists)
  199. {
  200. $this->setHandle();
  201. }
  202.  
  203. @fwrite($this->_workingHandle, $data);
  204. return;
  205.  
  206. }
  207.  
  208. /**
  209.   * Read a file out into an array without requiring a handle such as fopen,
  210.   * it uses the file function.
  211.   *
  212.   * @param int $limit Limit the amount of lines you want to be returned in the array
  213.   * @return array An array of elements including each individual line
  214.   */
  215. function lineByLine($limit=0)
  216. {
  217. // Setup the array of each line, and a count of total lines
  218. $arrayOfLines = file($this->_workingFile);
  219. $lineCount = count($arrayOfLines);
  220.  
  221. // If the user set a limit on how many lines they want returned
  222. if ($limit!=0)
  223. {
  224. // Loop through and unset the last element of the array
  225. // until it is to the desired line limit
  226. for ($i=$lineCount; $i>$limit; $i--)
  227. {
  228. unset($arrayOfLines[$i]);
  229. }
  230. }
  231.  
  232. return $arrayOfLines;
  233. }
  234.  
  235. function __destruct()
  236. {
  237. // If any handles exist, ie - a file was opened
  238. // and not closed, we close it now
  239. if ($this->_handleExists)
  240. {
  241. fclose($this->_workingHandle);
  242. }
  243. }
  244.  
  245. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.