7.x system.api.php hook_stream_wrappers()

Registers PHP stream wrapper implementations associated with a module.

Provide a facility for managing and querying user-defined stream wrappers in PHP. PHP's internal stream_get_wrappers() doesn't return the class registered to handle a stream, which we need to be able to find the handler for class instantiation.

If a module registers a scheme that is already registered with PHP, it will be unregistered and replaced with the specified class.

Return value

A nested array, keyed first by scheme name ("public" for "public://"), then keyed by the following values:

See also

file_get_stream_wrappers()

hook_stream_wrappers_alter()

system_stream_wrappers()

Related topics

3 functions implement hook_stream_wrappers()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

file_get_stream_wrappers in includes/file.inc
Provides Drupal stream wrapper registry.
file_test_stream_wrappers in modules/simpletest/tests/file_test.module
Implements hook_stream_wrappers().
system_stream_wrappers in modules/system/system.module
Implements hook_stream_wrappers().
1 invocation of hook_stream_wrappers()
file_get_stream_wrappers in includes/file.inc
Provides Drupal stream wrapper registry.

File

modules/system/system.api.php, line 2777
Hooks provided by Drupal core and the System module.

Code

function hook_stream_wrappers() {
  return array(
    'public' => array(
      'name' => t('Public files'),
      'class' => 'DrupalPublicStreamWrapper',
      'description' => t('Public local files served by the webserver.'),
      'type' => STREAM_WRAPPERS_LOCAL_NORMAL,
    ),
    'private' => array(
      'name' => t('Private files'),
      'class' => 'DrupalPrivateStreamWrapper',
      'description' => t('Private local files served by Drupal.'),
      'type' => STREAM_WRAPPERS_LOCAL_NORMAL,
    ),
    'temp' => array(
      'name' => t('Temporary files'),
      'class' => 'DrupalTempStreamWrapper',
      'description' => t('Temporary local files for upload and previews.'),
      'type' => STREAM_WRAPPERS_LOCAL_HIDDEN,
    ),
    'cdn' => array(
      'name' => t('Content delivery network files'),
      'class' => 'MyModuleCDNStreamWrapper',
      'description' => t('Files served by a content delivery network.'),
    ),
    'youtube' => array(
      'name' => t('YouTube video'),
      'class' => 'MyModuleYouTubeStreamWrapper',
      'description' => t('Video streamed from YouTube.'),
      // A module implementing YouTube integration may decide to support using
      // the YouTube API for uploading video, but here, we assume that this
      // particular module only supports playing YouTube video.
      'type' => STREAM_WRAPPERS_READ_VISIBLE,
    ),
  );
}

Comments

manarth’s picture

The PHP docs are pretty vague on the requirements, but trying to register a scheme-name with an underscore will result in the warning: Warning: stream_wrapper_register(): Invalid protocol scheme specified. Unable to register wrapper class. So no underscores then!

Dave Cohen’s picture

Thanks for the tip about avoiding underscores. Saved me some time.

B-Prod’s picture

The PHP implementation follows the RFC 3986 requirements:
http://tools.ietf.org/html/rfc3986#section-3.1

scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )

pingevt’s picture

I don't think this is technically a bug so I am posting it here. I had a hard time goggling it. If you create a stream wrapper, image cache might not work for images not using this. I created two menu items to handle files and image_cache. wrapper implementation and menu items below:

/**
 * Implements hook_stream_wrappers().
 */
function MY_MODULE_stream_wrappers() {
  return array(
    'overflow' => array(
      'name' => t('Overflow Files'),
      'class' => 'OverflowPublicStreamWrapper',
      'description' => t('Public local files served by the webserver on overflow drive.'),
      'type' => STREAM_WRAPPERS_LOCAL_NORMAL,
    ),
  );
}

class OverflowPublicStreamWrapper extends DrupalLocalStreamWrapper {
  public function getDirectoryPath() {
    return '/mnt/overflow/sixeight';
  }

  public function getExternalUrl() {
    $path = str_replace('\\', '/', $this->getTarget());
    return url('system/overflow/' . drupal_encode_path($path), array('absolute' => TRUE));
  }
}
  // Overflow Files
  $items['system/overflow'] = array(
    'title' => 'overflow files',
    'page callback' => 'file_download',
    'page arguments' => array('overflow'),
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );


  $items['system/overflow/styles/%image_style'] = array(
    'title' => 'Generate image style',
    'page callback' => 'image_style_deliver',
    'page arguments' => array(3),
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );

Hope this helps if anyone is confused.

izus’s picture

From 7.36 release note:
Added a 'file_public_schema' variable which allows modules that define publicly-accessible streams in hook_stream_wrappers() to bypass file download access checks when processing managed file upload fields.

progzy’s picture

This was removed in Drupal 8: https://www.drupal.org/node/2393323