public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
    {
        if (!in_array($mode, ['r', 'r+', 'w+', 'a+', 'c+'])) {
            throw new \InvalidArgumentException(sprintf('Invalid mode "%s" to read file "%s"', $mode));
        }

        if (($resource = @fopen($filename, $mode)) === false) {
            throw new \RuntimeException(sprintf('Unable to open file "%s" with mode "%s"', $filename, $mode));
        }

        // Read file
        rewind($resource);
        $content = fread($resource, filesize($filename));
        fclose($resource);

        return $this->createStream($content);
    }
public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
    {
        if (!in_array($mode, ['r', 'r+', 'w+', 'a+', 'c+'])) {
            throw new \InvalidArgumentException(sprintf('Invalid mode "%s" to read file "%s"', $mode));
        }

        if (($resource = @fopen($filename, $mode)) === false) {
            throw new \RuntimeException(sprintf('Unable to open file "%s" with mode "%s"', $filename, $mode));
        }

        // Read file
        rewind($resource);
        $content = fread($resource, filesize($filename));
        fclose($resource);

        return $this->createStream($content);
    }
public function createStreamFromFile ( string $ filename , string $ mode = 'r' ) : StreamInterface { if ( ! in_array ( $ mode , [ 'r' , 'r+' , 'w+' , 'a+' , 'c+' ] ) ) { throw new \ InvalidArgumentException ( sprintf ( 'Invalid mode "%s" to read file "%s"' , $ mode ) ) ; } if ( ( $ resource = @ fopen ( $ filename , $ mode ) ) === false ) { throw new \ RuntimeException ( sprintf ( 'Unable to open file "%s" with mode "%s"' , $ filename , $ mode ) ) ; } // Read file rewind ( $ resource ) ; $ content = fread ( $ resource , filesize ( $ filename ) ) ; fclose ( $ resource ) ; return $ this -> createStream ( $ content ) ; }
Create a stream from an existing file.

The file MUST be opened using the given mode, which may be any mode
supported by the `fopen` function.

The `$filename` MAY be any string supported by `fopen()`.

@param string $filename The filename or stream URI to use as basis of stream.
@param string $mode     The mode with which to open the underlying filename/stream.

@return \Psr\Http\Message\StreamInterface
@throws \RuntimeException If the file cannot be opened.
@throws \InvalidArgumentException If the mode is invalid.
Create a stream from an existing file .