Size
The Stream
components offer a few ways to determine the size of the underlying resource.
Raw size
Use getSize()
to obtain the stream's size in bytes. The method returns null
if the size is not known or cannot be determined.
// E.g. open file that 128 bytes in size
$stream = FileStream::open('persons.txt', 'r');
echo $stream->getSize(); // 128
Count
The Stream
components inherit from PHP's Countable
interface. This allows you to use count()
directly on the stream.
// E.g. open file that 51 bytes in size
$stream = FileStream::open('places.txt', 'r');
echo count($stream); // 51
Formatted Size
Use getFormattedSize()
if you wish to obtain a "human-readable" size of the stream.
The method returns a formatted string.
// E.g. open a "large" file...
$stream = FileStream::open('fx_lightning.mp4', 'r');
echo $stream->getFormattedSize(); // 4.3 MB