Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/StreamUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,26 @@ public static function getUsableUri($stream)
*
* @param resource $stream The stream.
*
* @return int The size of the stream.
* @return int|false The size of the stream, or false if it cannot be retrieved.
*/
public static function getSize($stream)
{
$stat = fstat($stream);
$stat = stream_get_meta_data($stream);

return $stat['size'];
switch ($stat['wrapper_type']) {
case 'plainfile':
$stats = fstat($stream);
return is_array($stats) && isset($stats['size']) ? $stats['size'] : false;
break;
case 'http':
stream_context_set_default(['http' => ['method' => 'HEAD']]);
$head = array_change_key_case(get_headers($stat['uri'], 1));
return $head['content-length'] ?? false;
break;
//@todo: Add logic for other wrapper types
default:
return false;
}
}

/**
Expand Down