function SessionStreamWrapper::url_stat

Support for stat().

This important function goes back to the Unix way of doing things. In this example almost the entire stat array is irrelevant, but the mode is very important. It tells PHP whether we have a file or a directory and what the permissions are. All that is packed up in a bitmask. This is not normal PHP fodder.

Parameters

string $uri: A string containing the URI to get information about.

int $flags: A bit mask of STREAM_URL_STAT_LINK and STREAM_URL_STAT_QUIET.

Return value

array|bool An array with file status, or FALSE in case of an error - see fstat() for a description of this array.

Overrides PhpStreamWrapperInterface::url_stat

See also

http://php.net/manual/en/streamwrapper.url-stat.php

File

modules/stream_wrapper_example/src/StreamWrapper/SessionStreamWrapper.php, line 741

Class

SessionStreamWrapper
Example stream wrapper class to handle session:// streams.

Namespace

Drupal\stream_wrapper_example\StreamWrapper

Code

public function url_stat($uri, $flags) {
  // @codingStandardsIgnoreEnd
  $path = $this->getLocalPath($uri);
  if (!$this->sessionHelper
    ->checkPath($path)) {
    return FALSE;
    // No file.
  }
  // Default to fail.
  $return = FALSE;
  $mode = 0;
  $key = $this->sessionHelper
    ->getPath($path);
  // We will call an array a directory and the root is always an array.
  if (is_array($key)) {
    // S_IFDIR means it's a directory.
    $mode = 040000;
  }
  elseif ($key !== FALSE) {
    // S_IFREG, means it's a file.
    $mode = 0100000;
  }
  if ($mode) {
    $size = 0;
    if ($mode == 0100000) {
      $size = strlen($key);
    }
    // There are no protections on this, so all writable.
    $mode |= 0777;
    $return = [
      'dev' => 0,
      'ino' => 0,
      'mode' => $mode,
      'nlink' => 0,
      'uid' => 0,
      'gid' => 0,
      'rdev' => 0,
      'size' => $size,
      'atime' => 0,
      'mtime' => 0,
      'ctime' => 0,
      'blksize' => 0,
      'blocks' => 0,
    ];
  }
  return $return;
}