diff --git a/vendor/magento/module-cms/Block/Adminhtml/Wysiwyg/Images/Tree.php b/vendor/magento/module-cms/Block/Adminhtml/Wysiwyg/Images/Tree.php
index c033e09ca8d..028a1a43276 100644
--- a/vendor/magento/module-cms/Block/Adminhtml/Wysiwyg/Images/Tree.php
+++ b/vendor/magento/module-cms/Block/Adminhtml/Wysiwyg/Images/Tree.php
@@ -76,8 +76,7 @@ class Tree extends \Magento\Backend\Block\Template
                 'path' => substr($item->getFilename(), strlen($storageRoot)),
                 'cls' => 'folder',
             ];
-            $nestedDirectories = $this->getMediaDirectory()->readRecursively($item->getFilename());
-            $hasNestedDirectories = count($nestedDirectories) > 0;
+            $hasNestedDirectories = $this->hasNestedDirectories($storageRoot, $item->getFilename());

             // if no nested directories inside dir, add 'leaf' state so that jstree hides dropdown arrow next to dir
             if (!$hasNestedDirectories) {
@@ -89,6 +88,26 @@ class Tree extends \Magento\Backend\Block\Template
         return $this->serializer->serialize($jsonArray);
     }

+    /**
+     * Check if directory has nested directories
+     *
+     * @param string $storageRoot
+     * @param string $fileName
+     * @return bool
+     */
+    private function hasNestedDirectories(string $storageRoot, string $fileName): bool
+    {
+        $pathList = $this->getMediaDirectory()->read($fileName);
+        foreach ($pathList as $directoryPath) {
+            $file = $this->_filesystem->getDirectoryReadByPath($storageRoot . $directoryPath);
+            if ($file->isDirectory()) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
     /**
      * Json source URL
      *
