Example: Files

Examples demonstrating the Drupal File API.

The File Example module is part of the Examples for Developers Project and provides a variety of examples for the Developers project page. Some concepts we demonstrate with this module:

  • Creating, moving and deleting files, and reading and writing from them.
  • Using files that Drupal can manage via its Entity API ("managed files"), and unmanaged files (the usual kind of file programs deal with).
  • Creating and setting up directories with the right permissions, and with .htaccess files that prevent unwanted accesses.
  • Allowing restricted access to files the way Drupal private files are downloaded.
  • Using special "stream" URIs like public://, private://, and temporary://. Drupal has good support for this PHP language feature. You can implement new file schemes as well; see the Stream Wrapper Example for how to do that.If you enable the stream_wrapper_example module, you can use it together with the File Example to test how a custom stream works.

To demonstrate all of this, the File Example implements a form that lets you play with files. Read src/Form/FileExampleReadWriteForm.php to see demonstrations of the various File API functions you will want to use in your code.

See also file for further information on the File API.

Parent topics

File

modules/file_example/file_example.module, line 3

Functions

Title Sort descending File name Summary
file_example_file_download modules/file_example/file_example.module Implements hook_file_download().

Classes

Title Sort descending File name Summary
FileExampleReadWriteForm modules/file_example/src/Form/FileExampleReadWriteForm.php File test form class.
FileExampleTest modules/file_example/tests/src/Functional/FileExampleTest.php Functional tests for the File Example module.