PHP code example of digitalcz / streams

1. Go to this page and download the library: Download digitalcz/streams 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/ */

    

digitalcz / streams example snippets


use DigitalCz\Streams\Stream;

// From a string
$stream = Stream::from('Hello world');
echo $stream->getContents();        // "Hello world"

// From an existing resource
$stream = Stream::from(fopen('php://memory', 'rb+'));

// From another PSR-7 stream
$stream = Stream::from($psrStream);

// Wrapping a resource directly (optionally with a known size)
$stream = new Stream(fopen('data.bin', 'rb'));

// Copy another stream into this one (returns bytes written)
$bytes = $stream->copy($otherStream);

use DigitalCz\Streams\File;

$file = new File('/path/to/file.txt');      // opened with mode "rb+" by default
echo $file->getPath();

// Build a file from a string / resource / PSR-7 stream.
// Write a string to a new temp file (the string is never treated as a path).
$file = File::fromString('contents written to a temp file');

// File::from() also accepts a string, but an existing path opens that file,
// otherwise it is the content. Never pass untrusted input to it.
$file = File::from($psrStreamOrResource);

$temp = File::temp();                        // empty file in the system temp dir
$file->delete();                             // close and unlink

use DigitalCz\Streams\TempFile;

$temp = TempFile::from('temporary content');
echo $temp->getPath();
$temp->close();                              // file is deleted on close

use DigitalCz\Streams\BufferedStream;

$buffered = new BufferedStream($nonSeekableSource);
$head = $buffered->read(1024);
$buffered->rewind();                         // works even if the source could not seek
$all = $buffered->getContents();

use DigitalCz\Streams\LimitStream;

$window = new LimitStream($stream, limit: 100, offset: 50);
$chunk = $window->getContents();             // bytes 50–149 of $stream

use DigitalCz\Streams\AppendStream;
use DigitalCz\Streams\Stream;

$stream = new AppendStream(Stream::from('header'), $body, Stream::from('footer'));
$all = $stream->getContents();

use DigitalCz\Streams\StreamWrapper;

$resource = StreamWrapper::from($stream);
$line = fgets($resource);

use DigitalCz\Streams\StreamInfo;

$sha256 = StreamInfo::hash($stream, 'sha256');   // any algorithm from hash_algos()
$mimeType = StreamInfo::mimeType($stream);        // e.g. "application/pdf",