You are here

function try_fopen in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/guzzlehttp/psr7/src/functions.php \GuzzleHttp\Psr7\try_fopen()

Safely opens a PHP stream resource using a filename.

When fopen fails, PHP normally raises a warning. This function adds an error handler that checks for errors and throws an exception instead.

Parameters

string $filename File to open:

string $mode Mode used to open the file:

Return value

resource

Throws

\RuntimeException if the file cannot be opened

1 call to try_fopen()
LazyOpenStream::createStream in vendor/guzzlehttp/psr7/src/LazyOpenStream.php
Creates the underlying stream lazily when required.

File

vendor/guzzlehttp/psr7/src/functions.php, line 271

Namespace

GuzzleHttp\Psr7

Code

function try_fopen($filename, $mode) {
  $ex = null;
  set_error_handler(function () use ($filename, $mode, &$ex) {
    $ex = new \RuntimeException(sprintf('Unable to open %s using mode %s: %s', $filename, $mode, func_get_args()[1]));
  });
  $handle = fopen($filename, $mode);
  restore_error_handler();
  if ($ex) {

    /** @var $ex \RuntimeException */
    throw $ex;
  }
  return $handle;
}