Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Utils.php

function Utils::tryFopen

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

5 calls to Utils::tryFopen()
CachingStream::__construct in vendor/guzzlehttp/psr7/src/CachingStream.php
We will treat the buffer object as the body of the stream
CurlFactory::applyHandlerOptions in vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php
HttpFactory::createStreamFromFile in vendor/guzzlehttp/psr7/src/HttpFactory.php
Create a stream from an existing file.
LazyOpenStream::createStream in vendor/guzzlehttp/psr7/src/LazyOpenStream.php
Creates the underlying stream lazily when required.
Utils::streamFor in vendor/guzzlehttp/psr7/src/Utils.php
Create a new stream based on the input type.

File

vendor/guzzlehttp/psr7/src/Utils.php, line 371

Class

Utils

Namespace

GuzzleHttp\Psr7

Code

public static function tryFopen(string $filename, string $mode) {
    $ex = null;
    set_error_handler(static function (int $errno, string $errstr) use ($filename, $mode, &$ex) : bool {
        $ex = new \RuntimeException(sprintf('Unable to open "%s" using mode "%s": %s', $filename, $mode, $errstr));
        return true;
    });
    try {
        
        /** @var resource $handle */
        $handle = fopen($filename, $mode);
    } catch (\Throwable $e) {
        $ex = new \RuntimeException(sprintf('Unable to open "%s" using mode "%s": %s', $filename, $mode, $e->getMessage()), 0, $e);
    }
    restore_error_handler();
    if ($ex) {
        
        /** @var $ex \RuntimeException */
        throw $ex;
    }
    return $handle;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal