PHP code example of sandromiguel / php-streams

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

    

sandromiguel / php-streams example snippets




use PhpStreams\Stream;

// Create a text stream in memory
$stream = new Stream(fopen('php://temp', 'r+'));

// Write data to the stream
$stream->write("Hello, world!\n");

// Move the pointer to the beginning of the stream
$stream->rewind();

// Read data from the stream
$data = $stream->getContents();

echo $data;



use PhpStreams\Stream;

// Open a file for reading
$fileHandle = fopen('example.txt', 'r');

// Create a stream from the file handle
$fileStream = new Stream($fileHandle);

// Check if the stream is readable
$fileContents = $fileStream->isReadable() ? $fileStream->getContents() : null;

if ($fileContents) {
  echo $fileContents;
} else {
  echo "The file is not readable.";
}

// Close the file handle
fclose($fileHandle);



use PhpStreams\Stream;

// Open a file for writing
$fileHandle = fopen('write.txt', 'w');

// Create a stream from the file handle
$fileStream = new Stream($fileHandle);

// Check if the stream is writable
$bytesWritten = $fileStream->isWritable() ? $fileStream->write('New text') : null;

if ($bytesWritten) {
  echo "Bytes written: $bytesWritten";
} else {
  echo "The file is not writable.";
}

// Close the file handle
fclose($fileHandle);



use PhpStreams\Stream;

// Open a file for reading
$fileHandle = fopen('example.txt', 'r');

// Create a stream from the file handle
$fileStream = new Stream($fileHandle);

// Check if the stream is readable
if ($fileStream->isReadable()) {
    // Define the exact number of bytes to read
    $numBytesToRead = 6;

    // Read 10 bytes from the file
    $data = $fileStream->read($numBytesToRead);

    // Output the read data
    echo "Read $numBytesToRead bytes of data: $data\n";

    // Read the remaining content of the file
    $remainingData = $fileStream->getContents();

    // Output the remaining data
    echo "Remaining data: $remainingData";
} else {
    echo "The file is not readable.";
}

// Close the file handle
fclose($fileHandle);



use PhpStreams\Stream;

// Open a file for reading
$fileHandle = fopen('example.txt', 'r');

// Create a stream from the file handle
$fileStream = new Stream($fileHandle);

// Get metadata of the stream
$metadata = $fileStream->getMetadata();

// Output the metadata
echo "Stream metadata:\n";
print_r($metadata);

// Close the file handle
fclose($fileHandle);
bash
composer