You are here

public function S3fsStream::stream_open in S3 File System 8.2

Same name and namespace in other branches
  1. 8.3 src/StreamWrapper/S3fsStream.php \Drupal\s3fs\StreamWrapper\S3fsStream::stream_open()
  2. 4.0.x src/StreamWrapper/S3fsStream.php \Drupal\s3fs\StreamWrapper\S3fsStream::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 PhpStreamWrapperInterface::stream_open

See also

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

File

src/StreamWrapper/S3fsStream.php, line 541

Class

S3fsStream
Defines a Drupal s3fs (s3fs://) stream wrapper class.

Namespace

Drupal\s3fs\StreamWrapper

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 = [];
  if (strpos($mode, '+')) {
    $errors[] = $this
      ->t('The S3 File System stream wrapper does not allow simultaneous reading and writing.');
  }
  if (!in_array($mode, [
    'r',
    'w',
    'a',
    'x',
  ])) {
    $errors[] = $this
      ->t("Mode not supported: %mode. Use one 'r', 'w', 'a', or 'x'.", [
      '%mode' => $mode,
    ]);
  }

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