You are here

public function S3fsStreamWrapper::stream_open in S3 File System 7

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

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

Parameters

string $uri: A string containing 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: A string containing the path actually opened.

Return value

bool Returns TRUE if file was opened successfully.

Overrides StreamWrapperInterface::stream_open

See also

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

File

./S3fsStreamWrapper.inc, line 534
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->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('s3://' . $this->params['Key'])) {
    $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;
}