/en/core-utility-libraries/file-folder.rst

https://github.com/jamiemill/docs · ReStructuredText · 436 lines · 212 code · 224 blank · 0 comment · 0 complexity · 496a74e21347538d170b641ef1619fb8 MD5 · raw file

  1. Folder & File
  2. #############
  3. The Folder and File utilities are convenience classes to help you read, write,
  4. and append to files; list files within a folder and other common directory
  5. related tasks.
  6. Basic usage
  7. ===========
  8. Ensure the classes are loaded using :php:meth:`App::uses()`::
  9. <?php
  10. App::uses('Folder', 'Utility');
  11. App::uses('File', 'Utility');
  12. Then we can setup a new folder instance::
  13. <?php
  14. $dir = new Folder('/path/to/folder');
  15. and search for all *.ctp* files within that folder using regex::
  16. <?php
  17. $files = $dir->find('.*\.ctp');
  18. Now we can loop through the files and read, write or append to the contents or
  19. simply delete the file::
  20. <?php
  21. foreach ($files as $file) {
  22. $file = new File($dir->pwd() . DS . $file);
  23. $contents = $file->read();
  24. // $file->write('I am overwriting the contents of this file');
  25. // $file->append('I am adding to the bottom of this file.');
  26. // $file->delete(); // I am deleting this file
  27. $file->close(); // Be sure to close the file when you're done
  28. }
  29. Folder API
  30. ==========
  31. .. php:class:: Folder
  32. .. php:staticmethod:: addPathElement( $path, $element )
  33. :rtype: string
  34. Returns $path with $element added, with correct slash in-between.::
  35. <?php
  36. $path = Folder::addPathElement('/a/path/for', 'testing');
  37. // $path equals /a/path/for/testing
  38. .. php:method:: cd( $path )
  39. :rtype: string
  40. Change directory to $path. Returns false on failure.
  41. .. php:method:: chmod( $path, $mode = false, $recursive = true, $exceptions = array ( ) )
  42. :rtype: boolean
  43. Change the mode on a directory structure recursively. This
  44. includes changing the mode on files as well.
  45. .. php:method:: copy( $options = array ( ) )
  46. :rtype: boolean
  47. Recursively copy a directory. The only parameter $options can
  48. either be a path into copy to or an array of options::
  49. <?php
  50. $folder1 = new Folder('/path/to/folder1');
  51. $folder1->copy('/path/to/folder2');
  52. // Will put folder1 and all its contents into folder2
  53. $folder = new Folder('/path/to/folder');
  54. $folder->copy(array(
  55. 'to' => '/path/to/new/folder',
  56. 'from' => '/path/to/copy/from', // will cause a cd() to occur
  57. 'mode' => 0755,
  58. 'skip' => array('skip-me.php', '.git'),
  59. ));
  60. .. php:staticmethod:: correctSlashFor( $path )
  61. :rtype: string
  62. Returns a correct set of slashes for given $path. (\\ for
  63. Windows paths and / for other paths.)
  64. .. php:method:: create( $pathname, $mode = false )
  65. :rtype: boolean
  66. Create a directory structure recursively. Can be used to create
  67. deep path structures like `/foo/bar/baz/shoe/horn`
  68. .. php:method:: delete( $path = NULL )
  69. :rtype: boolean
  70. Recursively Remove directories if the system allows.
  71. .. php:method:: dirsize( )
  72. :rtype: integer
  73. Returns the size in bytes of this Folder and its contents.
  74. .. php:method:: errors( )
  75. :rtype: array
  76. Get error from latest method.
  77. .. php:method:: find( $regexpPattern = '.*', $sort = false )
  78. :rtype: array
  79. Returns an array of all matching files in current directory.
  80. .. php:method:: findRecursive( $pattern = '.*', $sort = false )
  81. :rtype: array
  82. Returns an array of all matching files in and below current directory.
  83. .. php:method:: inCakePath( $path = '' )
  84. :rtype: boolean
  85. Returns true if the File is in a given CakePath.
  86. .. php:method:: inPath( $path = '', $reverse = false )
  87. :rtype: boolean
  88. Returns true if the File is in given path.
  89. .. php:staticmethod:: isAbsolute( $path )
  90. :rtype: boolean
  91. Returns true if given $path is an absolute path.
  92. .. php:staticmethod:: isSlashTerm( $path )
  93. :rtype: boolean
  94. Returns true if given $path ends in a slash (i.e. is slash-terminated).
  95. .. php:staticmethod:: isWindowsPath( $path )
  96. :rtype: boolean
  97. Returns true if given $path is a Windows path.
  98. .. php:method:: messages( )
  99. :rtype: array
  100. Get messages from latest method.
  101. .. php:method:: move( $options )
  102. :rtype: boolean
  103. Recursive directory move.
  104. .. php:staticmethod:: normalizePath( $path )
  105. :rtype: string
  106. Returns a correct set of slashes for given $path. (\\ for
  107. Windows paths and / for other paths.)
  108. .. php:method:: pwd( )
  109. :rtype: string
  110. Return current path.
  111. .. php:method:: read( $sort = true, $exceptions = false, $fullPath = false )
  112. :rtype: mixed
  113. Returns an array of the contents of the current directory. The
  114. returned array holds two arrays: One of directories and one of files.
  115. .. php:method:: realpath( $path )
  116. :rtype: string
  117. Get the real path (taking ".." and such into account).
  118. .. php:staticmethod:: slashTerm( $path )
  119. :rtype: string
  120. Returns $path with added terminating slash (corrected for
  121. Windows or other OS).
  122. .. php:method:: tree( $path = NULL, $exceptions = true, $type = NULL )
  123. :rtype: mixed
  124. Returns an array of nested directories and files in each directory.
  125. File API
  126. ========
  127. .. php:class:: File
  128. .. php:method:: append( $data, $force = false )
  129. :rtype: boolean
  130. Append given data string to this File.
  131. .. php:method:: close( )
  132. :rtype: boolean
  133. Closes the current file if it is opened.
  134. .. php:method:: copy( $dest, $overwrite = true )
  135. :rtype: boolean
  136. Copy the File to $dest
  137. .. php:method:: create( )
  138. :rtype: boolean
  139. Creates the File.
  140. .. php:method:: delete( )
  141. :rtype: boolean
  142. Deletes the File.
  143. .. php:method:: executable( )
  144. :rtype: boolean
  145. Returns true if the File is executable.
  146. .. php:method:: exists( )
  147. :rtype: boolean
  148. Returns true if the File exists.
  149. .. php:method:: ext( )
  150. :rtype: string
  151. Returns the File extension.
  152. .. php:method:: Folder( )
  153. :rtype: Folder
  154. Returns the current folder.
  155. .. php:method:: group( )
  156. :rtype: integer
  157. Returns the File's group.
  158. .. php:method:: info( )
  159. :rtype: string
  160. Returns the File info.
  161. .. php:method:: lastAccess( )
  162. :rtype: integer
  163. Returns last access time.
  164. .. php:method:: lastChange( )
  165. :rtype: integer
  166. Returns last modified time.
  167. .. php:method:: md5( $maxsize = 5 )
  168. :rtype: string
  169. Get md5 Checksum of file with previous check of Filesize
  170. .. php:method:: name( )
  171. :rtype: string
  172. Returns the File name without extension.
  173. .. php:method:: offset( $offset = false, $seek = 0 )
  174. :rtype: mixed
  175. Sets or gets the offset for the currently opened file.
  176. .. php:method:: open( $mode = 'r', $force = false )
  177. :rtype: boolean
  178. Opens the current file with a given $mode
  179. .. php:method:: owner( )
  180. :rtype: integer
  181. Returns the File's owner.
  182. .. php:method:: perms( )
  183. :rtype: string
  184. Returns the "chmod" (permissions) of the File.
  185. .. php:staticmethod:: prepare( $data, $forceWindows = false )
  186. :rtype: string
  187. Prepares a ascii string for writing. Converts line endings to the
  188. correct terminator for the current platform. If windows "\r\n"
  189. will be used all other platforms will use "\n"
  190. .. php:method:: pwd( )
  191. :rtype: string
  192. Returns the full path of the File.
  193. .. php:method:: read( $bytes = false, $mode = 'rb', $force = false )
  194. :rtype: mixed
  195. Return the contents of this File as a string or return false on failure.
  196. .. php:method:: readable( )
  197. :rtype: boolean
  198. Returns true if the File is readable.
  199. .. php:method:: safe( $name = NULL, $ext = NULL )
  200. :rtype: string
  201. Makes filename safe for saving.
  202. .. php:method:: size( )
  203. :rtype: integer
  204. Returns the Filesize.
  205. .. php:method:: writable( )
  206. :rtype: boolean
  207. Returns true if the File is writable.
  208. .. php:method:: write( $data, $mode = 'w', $force = false )
  209. :rtype: boolean
  210. Write given data to this File.
  211. .. todo::
  212. Better explain how to use each method with both classes.