You are here

public function S3fsStreamWrapper::stream_open in S3 File System 7.2

Same name and namespace in other branches
  1. 7.3 S3fsStreamWrapper.inc \S3fsStreamWrapper::stream_open()
  2. 7 S3fsStreamWrapper.inc \S3fsStreamWrapper::stream_open()

Support for fopen(), file_get_contents(), file_put_contents() etc.

Parameters

string $uri: The URI of the file to open.

string $mode: The file mode. Only 'r', 'w', 'a', and 'x' are supported.

int $options: A bit mask of STREAM_USE_PATH and STREAM_REPORT_ERRORS.

string $opened_path: An OUT parameter populated with the path which was opened. This wrapper does not support this parameter.

Return value

bool TRUE if file was opened successfully. Otherwise, FALSE.

Overrides StreamWrapperInterface::stream_open

See also

http://php.net/manual/en/streamwrapper.stream-open.php

File

./S3fsStreamWrapper.inc, line 605
Drupal stream wrapper implementation for S3 File System.

Class

S3fsStreamWrapper
The stream wrapper class.

Code

public function stream_open($uri, $mode, $options, &$opened_path) {
  $this
    ->_debug("stream_open({$uri}, {$mode}, {$options}, {$opened_path}) called.");
  $this->uri = $uri;

  // We don't care about the binary flag, so strip it out.
  $this->access_mode = $mode = rtrim($mode, 'bt');
  $this->params = $this
    ->_get_params($uri);
  $errors = array();
  if (strpos($mode, '+')) {
    $errors[] = t('The S3 File System stream wrapper does not allow simultaneous reading and writing.');
  }
  if (!in_array($mode, array(
    'r',
    'w',
    'a',
    'x',
  ))) {
    $errors[] = t("Mode not supported: %mode. Use one 'r', 'w', 'a', or 'x'.", array(
      '%mode' => $mode,
    ));
  }

  // When using mode "x", validate if the file exists first.
  if ($mode == 'x' && $this
    ->_read_cache($uri)) {
    $errors[] = t("%uri already exists in your S3 bucket, so it cannot be opened with mode 'x'.", array(
      '%uri' => $uri,
    ));
  }
  if (!$errors) {
    if ($mode == 'r') {
      $this
        ->_open_read_stream($this->params, $errors);
    }
    elseif ($mode == 'a') {
      $this
        ->_open_append_stream($this->params, $errors);
    }
    else {
      $this
        ->_open_write_stream($this->params, $errors);
    }
  }
  return $errors ? $this
    ->_trigger_error($errors) : TRUE;
}