public static function tryFopen(string $filename, string $mode)
    {
        $ex = null;

        \set_error_handler(static function () use ($filename, $mode, &$ex): void {
            $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 instanceof Throwable) {
            // @var $ex \RuntimeException
            throw $ex;
        }

        return $handle;
    }
public static function tryFopen(string $filename, string $mode)
    {
        $ex = null;

        \set_error_handler(static function () use ($filename, $mode, &$ex): void {
            $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 instanceof Throwable) {
            // @var $ex \RuntimeException
            throw $ex;
        }

        return $handle;
    }
public static function tryFopen ( string $ filename , string $ mode ) { $ ex = null ; \ set_error_handler ( static function ( ) use ( $ filename , $ mode , & $ ex ) : void { $ 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 instanceof Throwable ) { // @var $ex \RuntimeException throw $ ex ; } return $ handle ; }
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.

@param string $filename File to open
@param string $mode     Mode used to open the file

@throws \Viserio\Component\Contract\Http\Exception\RuntimeException if the file cannot be opened

@return false|resource
Safely opens a PHP stream resource using a filename .