diff --git a/lib/private/Files/Stream/Encryption.php b/lib/private/Files/Stream/Encryption.php
index 05be5a5b286e0c016807c4da6abfc34361cffbbc..65d379c028920332e5edc82bbd30f6dcc0f8a413 100644
--- a/lib/private/Files/Stream/Encryption.php
+++ b/lib/private/Files/Stream/Encryption.php
@@ -195,10 +195,10 @@ class Encryption extends Wrapper {
 	protected static function wrapSource($source, $context, $protocol, $class, $mode = 'r+') {
 		try {
 			stream_wrapper_register($protocol, $class);
-			if (@rewinddir($source) === false) {
-				$wrapped = fopen($protocol . '://', $mode, false, $context);
-			} else {
+			if (self::isDirectoryHandle($source)) {
 				$wrapped = opendir($protocol . '://', $context);
+			} else {
+				$wrapped = fopen($protocol . '://', $mode, false, $context);
 			}
 		} catch (\BadMethodCallException $e) {
 			stream_wrapper_unregister($protocol);