file_example.module 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * @file
  4. * Examples demonstrating the Drupal File API.
  5. */
  6. /**
  7. * @defgroup file_example Example: Files
  8. * @ingroup examples
  9. * @{
  10. * Examples demonstrating the Drupal File API.
  11. *
  12. * The File Example module is part of the Examples for Developers Project and
  13. * provides a variety of examples for the Developers project page. Some
  14. * concepts we demonstrate with this module:
  15. *
  16. * * Creating, moving and deleting files, and reading and writing from them.
  17. *
  18. * * Using files that Drupal can manage via its Entity API ("managed files"),
  19. * and unmanaged files (the usual kind of file programs deal with).
  20. *
  21. * * Creating and setting up directories with the right permissions, and with
  22. * .htaccess files that prevent unwanted accesses.
  23. *
  24. * * Allowing restricted access to files the way Drupal private files are
  25. * downloaded.
  26. *
  27. * * Using special "stream" URIs like public://, private://, and temporary://.
  28. * Drupal has good support for this PHP language feature. You can implement
  29. * new file schemes as well; see the Stream Wrapper Example for how to do
  30. * that.If you enable the stream_wrapper_example module, you can use it
  31. * together with the File Example to test how a custom stream works.
  32. *
  33. * To demonstrate all of this, the File Example implements a form that lets you
  34. * play with files. Read src/Form/FileExampleReadWriteForm.php to see
  35. * demonstrations of the various File API functions you will want to use in your
  36. * code.
  37. *
  38. * Some links for further information on the File API and related information:
  39. *
  40. * @link http://drupal.org/project/examples Examples for Developers project
  41. * page. @endlink
  42. * @link file File summary on api.drupal.org @endlink for the function summary.
  43. */
  44. /**
  45. * Implements hook_file_download().
  46. *
  47. * This hook allows modules to enforce permissions on file downloads whenever
  48. * Drupal is handling file download, as opposed to the web server bypassing
  49. * Drupal and returning the file from a public directory. Modules can also
  50. * provide headers to specify information like the file's name or MIME type.
  51. *
  52. * For our example module, we want to be able to see the temporary, private,
  53. * and session (our test stream wrapper / file scheme). In general, you really
  54. * would NEVER give general access to your temporary, and you certainly wouldn't
  55. * do it for your private files. So we demonstrate this here, but kids, don't
  56. * try this at home ;-) Remember: keep your files secure!
  57. *
  58. * For hook_file_download() to get called at all, your code needs set up your
  59. * routes so that the download link uses FileDownloadController::download() as
  60. * a controller. FileDownloadController::download() enforces access restrictions
  61. * on the files it managed, in part by invoking hook_file_downloads(). See the
  62. * File Example's routing file to see how to do this.
  63. *
  64. * @param string $uri
  65. * The URI of the file.
  66. *
  67. * @return mixed
  68. * If the user does not have permission to access the file, return -1. If the
  69. * user has permission, return an array with the appropriate headers. If the
  70. * file is not controlled by the current module, the return value should be
  71. * NULL.
  72. *
  73. * @see file_download()
  74. * @see hook_file_download()
  75. * @see file_example.routing.yml
  76. * @see \Drupal\system\FileDownloadController::download()
  77. */
  78. function file_example_file_download($uri) {
  79. $scheme = file_uri_scheme($uri);
  80. if (in_array($scheme, ['private', 'temporary', 'session'])) {
  81. $permission = "read $scheme files";
  82. $current_user = \Drupal::currentUser();
  83. $account = $current_user->getAccount();
  84. if ($account->hasPermission($permission)) {
  85. return [
  86. 'Content-Type: text/plain',
  87. ];
  88. }
  89. }
  90. }
  91. /**
  92. * @} End of "defgroup file_example".
  93. */