FileSystemInterface.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. namespace Drupal\Core\File;
  3. /**
  4. * Provides an interface for helpers that operate on files and stream wrappers.
  5. */
  6. interface FileSystemInterface {
  7. /**
  8. * Moves an uploaded file to a new location.
  9. *
  10. * PHP's move_uploaded_file() does not properly support streams if
  11. * open_basedir is enabled, so this function fills that gap.
  12. *
  13. * Compatibility: normal paths and stream wrappers.
  14. *
  15. * @param string $filename
  16. * The filename of the uploaded file.
  17. * @param string $uri
  18. * A string containing the destination URI of the file.
  19. *
  20. * @return bool
  21. * TRUE on success, or FALSE on failure.
  22. *
  23. * @see move_uploaded_file()
  24. * @see https://www.drupal.org/node/515192
  25. * @ingroup php_wrappers
  26. */
  27. public function moveUploadedFile($filename, $uri);
  28. /**
  29. * Sets the permissions on a file or directory.
  30. *
  31. * This function will use the file_chmod_directory and
  32. * file_chmod_file settings for the default modes for directories
  33. * and uploaded/generated files. By default these will give everyone read
  34. * access so that users accessing the files with a user account without the
  35. * webserver group (e.g. via FTP) can read these files, and give group write
  36. * permissions so webserver group members (e.g. a vhost account) can alter
  37. * files uploaded and owned by the webserver.
  38. *
  39. * PHP's chmod does not support stream wrappers so we use our wrapper
  40. * implementation which interfaces with chmod() by default. Contrib wrappers
  41. * may override this behavior in their implementations as needed.
  42. *
  43. * @param string $uri
  44. * A string containing a URI file, or directory path.
  45. * @param int $mode
  46. * Integer value for the permissions. Consult PHP chmod() documentation for
  47. * more information.
  48. *
  49. * @return bool
  50. * TRUE for success, FALSE in the event of an error.
  51. *
  52. * @ingroup php_wrappers
  53. */
  54. public function chmod($uri, $mode = NULL);
  55. /**
  56. * Deletes a file.
  57. *
  58. * PHP's unlink() is broken on Windows, as it can fail to remove a file when
  59. * it has a read-only flag set.
  60. *
  61. * @param string $uri
  62. * A URI or pathname.
  63. * @param resource $context
  64. * Refer to http://php.net/manual/ref.stream.php
  65. *
  66. * @return bool
  67. * Boolean TRUE on success, or FALSE on failure.
  68. *
  69. * @see unlink()
  70. * @ingroup php_wrappers
  71. */
  72. public function unlink($uri, $context = NULL);
  73. /**
  74. * Resolves the absolute filepath of a local URI or filepath.
  75. *
  76. * The use of this method is discouraged, because it does not work for
  77. * remote URIs. Except in rare cases, URIs should not be manually resolved.
  78. *
  79. * Only use this function if you know that the stream wrapper in the URI uses
  80. * the local file system, and you need to pass an absolute path to a function
  81. * that is incompatible with stream URIs.
  82. *
  83. * @param string $uri
  84. * A stream wrapper URI or a filepath, possibly including one or more
  85. * symbolic links.
  86. *
  87. * @return string|false
  88. * The absolute local filepath (with no symbolic links) or FALSE on failure.
  89. *
  90. * @see \Drupal\Core\StreamWrapper\StreamWrapperInterface::realpath()
  91. * @see http://php.net/manual/function.realpath.php
  92. * @ingroup php_wrappers
  93. */
  94. public function realpath($uri);
  95. /**
  96. * Gets the name of the directory from a given path.
  97. *
  98. * PHP's dirname() does not properly pass streams, so this function fills that
  99. * gap. It is backwards compatible with normal paths and will use PHP's
  100. * dirname() as a fallback.
  101. *
  102. * Compatibility: normal paths and stream wrappers.
  103. *
  104. * @param string $uri
  105. * A URI or path.
  106. *
  107. * @return string
  108. * A string containing the directory name.
  109. *
  110. * @see dirname()
  111. * @see https://www.drupal.org/node/515192
  112. * @ingroup php_wrappers
  113. */
  114. public function dirname($uri);
  115. /**
  116. * Gets the filename from a given path.
  117. *
  118. * PHP's basename() does not properly support streams or filenames beginning
  119. * with a non-US-ASCII character.
  120. *
  121. * @see http://bugs.php.net/bug.php?id=37738
  122. * @see basename()
  123. *
  124. * @ingroup php_wrappers
  125. */
  126. public function basename($uri, $suffix = NULL);
  127. /**
  128. * Creates a directory, optionally creating missing components in the path to
  129. * the directory.
  130. *
  131. * When PHP's mkdir() creates a directory, the requested mode is affected by
  132. * the process's umask. This function overrides the umask and sets the mode
  133. * explicitly for all directory components created.
  134. *
  135. * @param string $uri
  136. * A URI or pathname.
  137. * @param int $mode
  138. * Mode given to created directories. Defaults to the directory mode
  139. * configured in the Drupal installation. It must have a leading zero.
  140. * @param bool $recursive
  141. * Create directories recursively, defaults to FALSE. Cannot work with a
  142. * mode which denies writing or execution to the owner of the process.
  143. * @param resource $context
  144. * Refer to http://php.net/manual/ref.stream.php
  145. *
  146. * @return bool
  147. * Boolean TRUE on success, or FALSE on failure.
  148. *
  149. * @see mkdir()
  150. * @see https://www.drupal.org/node/515192
  151. * @ingroup php_wrappers
  152. *
  153. * @todo Update with open_basedir compatible recursion logic from
  154. * \Drupal\Component\PhpStorage\FileStorage::ensureDirectory().
  155. */
  156. public function mkdir($uri, $mode = NULL, $recursive = FALSE, $context = NULL);
  157. /**
  158. * Removes a directory.
  159. *
  160. * PHP's rmdir() is broken on Windows, as it can fail to remove a directory
  161. * when it has a read-only flag set.
  162. *
  163. * @param string $uri
  164. * A URI or pathname.
  165. * @param resource $context
  166. * Refer to http://php.net/manual/ref.stream.php
  167. *
  168. * @return bool
  169. * Boolean TRUE on success, or FALSE on failure.
  170. *
  171. * @see rmdir()
  172. * @ingroup php_wrappers
  173. */
  174. public function rmdir($uri, $context = NULL);
  175. /**
  176. * Creates a file with a unique filename in the specified directory.
  177. *
  178. * PHP's tempnam() does not return a URI like we want. This function will
  179. * return a URI if given a URI, or it will return a filepath if given a
  180. * filepath.
  181. *
  182. * Compatibility: normal paths and stream wrappers.
  183. *
  184. * @param string $directory
  185. * The directory where the temporary filename will be created.
  186. * @param string $prefix
  187. * The prefix of the generated temporary filename.
  188. * Note: Windows uses only the first three characters of prefix.
  189. *
  190. * @return string|bool
  191. * The new temporary filename, or FALSE on failure.
  192. *
  193. * @see tempnam()
  194. * @see https://www.drupal.org/node/515192
  195. * @ingroup php_wrappers
  196. */
  197. public function tempnam($directory, $prefix);
  198. /**
  199. * Returns the scheme of a URI (e.g. a stream).
  200. *
  201. * @param string $uri
  202. * A stream, referenced as "scheme://target" or "data:target".
  203. *
  204. * @return string|bool
  205. * A string containing the name of the scheme, or FALSE if none. For
  206. * example, the URI "public://example.txt" would return "public".
  207. *
  208. * @see file_uri_target()
  209. */
  210. public function uriScheme($uri);
  211. /**
  212. * Checks that the scheme of a stream URI is valid.
  213. *
  214. * Confirms that there is a registered stream handler for the provided scheme
  215. * and that it is callable. This is useful if you want to confirm a valid
  216. * scheme without creating a new instance of the registered handler.
  217. *
  218. * @param string $scheme
  219. * A URI scheme, a stream is referenced as "scheme://target".
  220. *
  221. * @return bool
  222. * Returns TRUE if the string is the name of a validated stream, or FALSE if
  223. * the scheme does not have a registered handler.
  224. */
  225. public function validScheme($scheme);
  226. }