From c0959bb8e672d56b0157c550e561f688d30fc291 Mon Sep 17 00:00:00 2001 From: Lisa Ridley Date: Wed, 24 Mar 2021 18:23:45 -0400 Subject: [PATCH] fstat() returning a boolean value instead of an array This commit patches the master branch (which at the time of this MR is the same as verson v1.0.1) to better handle a boolean return value from fstat(), which can happen with remote stream wrappers. --- src/StreamUtil.php | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/StreamUtil.php b/src/StreamUtil.php index 09907ef..342caa1 100644 --- a/src/StreamUtil.php +++ b/src/StreamUtil.php @@ -84,13 +84,27 @@ public static function getUsableUri($stream) * * @param resource $stream The stream. * - * @return int The size of the stream. + * @return mixed 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']; + if ($stat['wrapper_type'] == 'plainfile') { + $stats = fstat($stream); + // $stats['size'] is not set, return false. + return is_array($stats) && isset($stats['size']) ? $stats['size'] : false; + } + + if ($stat['wrapper_type'] == 'http') { + stream_context_set_default(array('http' => array('method' => 'HEAD'))); + $head = array_change_key_case(get_headers($stat['uri'], 1)); + // Lowercase "content-length: of download (in bytes), read from Content-Length: field. + // If not set, return false. + return isset($head['content-length']) ? $head['content-length'] : false; + } + // @todo: Add logic for other wrapper types. + return false; } /**