The Stream class allows you to interact with data streams in PHP, providing a simple interface for reading and writing data to stream resources.
Note
When working with streams, it is important to keep in mind that read and write operations can be more efficient than loading all content into memory, especially with large files. Using methods such as read and write allows for more controlled handling of data.
For more information on PHP's stream handling, you can refer to the official PHP documentation on Streams.
To create a new instance of the Stream class, you must provide a stream resource or a string representing the path to a file, along with the opening mode of the stream.
require __DIR__ . '/vendor/autoload.php';
$stream = new Mk4U\Http\Stream('path/to/file.txt', 'r+');The available opening modes are:
rReadrbRead in binary modertRead in text moder+Read and writerb+Read and write in binary modert+Read and write in binary modert+Read and write in binary modert+Read and write in text modea+Write (aggregate) and readab+Write (append) and read in binary modew+Write and readwbWrite and read in binary modex+Create and write (will fail if the file already exists)xb+Create and write in binary mode (will fail if the file already exists)c+Write (truncate) and readcb+Write (truncate) and read in binary modewWrite (truncate)wbWrite in binary mode (truncate)wtWrite in text mode (truncate)aWrite (append)abWrite (append) in binary modeatWrite (append) in text modecWrite (truncate)xCreate and write (will fail if the file already exists)r+Reading and writingrb+Read and write in binary moderwRead and write (not a standard mode in PHP, but is included here for reference)c+Write (truncate) and read
The __toString magic method returns the strean converted to a string.
echo $stream; // returns the contents of the streamCloses the flow and releases any underlying resource.
$stream->close();Separates the underlying resource from the flow and returns it.
$resource = $stream->detach();Gets the size of the stream, if known.
$size = $stream->getSize(); // returns the size in bytesReturns the current position of the file read/write pointer.
$position = $stream->tell(); // returns the current positionReturns true if the pointer is at the end of the stream.
$isEnd = $stream->eof(); // returns true if at the endReturns whether the stream is searchable or not.
$isSeekable = $stream->isSeekable(); // returns true or falseSearches for a position in the stream.
$stream->seek(0); // return to the beginning of the flowReturns to the beginning of the stream.
$stream->rewind();Returns whether the stream is writable.
$isWritable = $stream->isWritable(); // returns true or falseWrites data to the stream.
$bytesWritten = $stream->write("Nuevo contenido"); // returns the number of bytes writtenReturns whether the stream is readable or not.
$isReadable = $stream->isReadable(); // returns true or falseReads data from the stream.
$data = $stream->read(10); // reads up to 10 bytesReturns the remaining content in a string.
$contents = $stream->getContents(); // returns the remaining contentsGets metadata from the stream.
$metadata = $stream->getMetadata(); // returns all metadata
$mode = $stream->getMetadata('mode'); // returns the flow modetry {
$stream = new Mk4U\Http\Stream('path/to/file.txt', 'r');
echo $stream->getContents();
$stream->close();
} catch (\Exception $e) {
echo "Error: " . $e->getMessage();
}try {
$stream = new Mk4U\Http\Stream('path/to/file.txt', 'a+');
$stream->write("Hello World\n");
$stream->close();
} catch (\Exception $e) {
echo "Error: " . $e->getMessage();
}try {
$stream = new Mk4U\Http\Stream('path/to/file.txt', 'r+');
// go to the beginning of the file
$stream->seek(0);
// read the first 10 bytes
echo $stream->read(10);
// go back to the beginning of the file
$stream->rewind();
// read the entire contents of the file
$content = $stream->read($stream->getSize());
echo $content;
// close the stream
$stream->close();
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}