PHP code example of phpro / resource-stream

1. Go to this page and download the library: Download phpro/resource-stream library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

phpro / resource-stream example snippets


use Phpro\ResourceStream\ResourceStream;

$stream = (new ResourceStream(fopen('php://temp', 'r+')))
    ->write('Hello World')
    ->rewind();

// Various ways to read:
echo $stream->read();
echo $stream->read($bufferSize);
echo $stream->readLine();
echo $stream->readLine($bufferSize, ending: \PHP_EOL);
echo $stream->getContents();

// Or in batches (Generator<string>)
$cursor = $stream->readBatches($bufferSize);
$cursor = $stream->readLines($bufferSize, ending: \PHP_EOL); 

// Get access to PHP's inner resource stream
$innerStream = $stream->unwrap();

// Get access to common information:
$stream->isOpen();
$stream->isEof();
$stream->uri();
$stream->size();

// Possibility to copy contents across streams
$stream->copyTo($anotherStream);
$stream->copyFrom($anotherStream);

// Streams will automatically be closed on destruction.
// Of course, you can choose to keep it open or close it manually:
$stream->keepAlive();
$stream->close();

use Phpro\ResourceStream\Factory\FileStream;

$stream = FileStream::create('/path/to/file', FileStream::READ_WRITE_MODE);

use Phpro\ResourceStream\Factory\MemoryStream;

$stream = MemoryStream::create();

use Phpro\ResourceStream\Factory\Psr7Stream;

$stream = Psr7Stream::createFromStream($anyPsr7Stream);
$stream = Psr7Stream::createFromRequest($anyPsr7Request);
$stream = Psr7Stream::createFromResponse($anyPsr7Response);

use Phpro\ResourceStream\Factory\TmpStream;

$stream = TmpStream::create();