diff --git a/apps/files_encryption/lib/util.php b/apps/files_encryption/lib/util.php
index b8d6862349395efa1a02b7039b267cee87faa700..8a7b6f40cba25b4855b3c9d074c75685a0cccbc9 100644
--- a/apps/files_encryption/lib/util.php
+++ b/apps/files_encryption/lib/util.php
@@ -329,72 +329,73 @@ class Util {
 			$this->view->is_dir($directory)
 			&& $handle = $this->view->opendir($directory)
 		) {
-
-			while (false !== ($file = readdir($handle))) {
-
-				if (
-					$file !== "."
-					&& $file !== ".."
-				) {
-
-					$filePath = $directory . '/' . $this->view->getRelativePath('/' . $file);
-					$relPath = \OCA\Encryption\Helper::stripUserFilesPath($filePath);
-
-					// If the path is a directory, search
-					// its contents
-					if ($this->view->is_dir($filePath)) {
-
-						$this->findEncFiles($filePath, $found);
-
-						// If the path is a file, determine
-						// its encryption status
-					} elseif ($this->view->is_file($filePath)) {
-
-						// Disable proxies again, some-
-						// where they got re-enabled :/
-						\OC_FileProxy::$enabled = false;
-
-						$isEncryptedPath = $this->isEncryptedPath($filePath);
-						// If the file is encrypted
-						// NOTE: If the userId is
-						// empty or not set, file will
-						// detected as plain
-						// NOTE: This is inefficient;
-						// scanning every file like this
-						// will eat server resources :(
-						if (
-							Keymanager::getFileKey($this->view, $this->userId, $relPath)
-							&& $isEncryptedPath
-						) {
-
-							$found['encrypted'][] = array(
-								'name' => $file,
-								'path' => $filePath
-							);
-
-							// If the file uses old
-							// encryption system
-						} elseif (Crypt::isLegacyEncryptedContent($isEncryptedPath, $relPath)) {
-
-							$found['legacy'][] = array(
-								'name' => $file,
-								'path' => $filePath
-							);
-
-							// If the file is not encrypted
-						} else {
-
-							$found['plain'][] = array(
-								'name' => $file,
-								'path' => $relPath
-							);
+			if(is_resource($handle)) {
+				while (false !== ($file = readdir($handle))) {
+
+					if (
+						$file !== "."
+						&& $file !== ".."
+					) {
+
+						$filePath = $directory . '/' . $this->view->getRelativePath('/' . $file);
+						$relPath = \OCA\Encryption\Helper::stripUserFilesPath($filePath);
+
+						// If the path is a directory, search
+						// its contents
+						if ($this->view->is_dir($filePath)) {
+
+							$this->findEncFiles($filePath, $found);
+
+							// If the path is a file, determine
+							// its encryption status
+						} elseif ($this->view->is_file($filePath)) {
+
+							// Disable proxies again, some-
+							// where they got re-enabled :/
+							\OC_FileProxy::$enabled = false;
+
+							$isEncryptedPath = $this->isEncryptedPath($filePath);
+							// If the file is encrypted
+							// NOTE: If the userId is
+							// empty or not set, file will
+							// detected as plain
+							// NOTE: This is inefficient;
+							// scanning every file like this
+							// will eat server resources :(
+							if (
+								Keymanager::getFileKey($this->view, $this->userId, $relPath)
+								&& $isEncryptedPath
+							) {
+
+								$found['encrypted'][] = array(
+									'name' => $file,
+									'path' => $filePath
+								);
+
+								// If the file uses old
+								// encryption system
+							} elseif (Crypt::isLegacyEncryptedContent($isEncryptedPath, $relPath)) {
+
+								$found['legacy'][] = array(
+									'name' => $file,
+									'path' => $filePath
+								);
+
+								// If the file is not encrypted
+							} else {
+
+								$found['plain'][] = array(
+									'name' => $file,
+									'path' => $relPath
+								);
+
+							}
 
 						}
 
 					}
 
 				}
-
 			}
 
 			\OC_FileProxy::$enabled = true;
diff --git a/apps/files_external/lib/config.php b/apps/files_external/lib/config.php
index 1935740cd2e4a5748c4e1763cf73093885d26c58..659959e662e439fcbd5ca80878fd482b277b4c5f 100755
--- a/apps/files_external/lib/config.php
+++ b/apps/files_external/lib/config.php
@@ -378,7 +378,7 @@ class OC_Mount_Config {
 		}
 		$result = array();
 		$handle = opendir($path);
-		if ( ! $handle) {
+		if(!is_resource($handle)) {
 			return array();
 		}
 		while (false !== ($file = readdir($handle))) {
diff --git a/apps/files_sharing/lib/sharedstorage.php b/apps/files_sharing/lib/sharedstorage.php
index d91acbbb2bdb708d2de8bf58705c667fe536a23e..257da89c84e52e3a68ae27d4282109301b461563 100644
--- a/apps/files_sharing/lib/sharedstorage.php
+++ b/apps/files_sharing/lib/sharedstorage.php
@@ -221,7 +221,8 @@ class Shared extends \OC\Files\Storage\Common {
 	public function filemtime($path) {
 		if ($path == '' || $path == '/') {
 			$mtime = 0;
-			if ($dh = $this->opendir($path)) {
+			$dh = $this->opendir($path);
+			if(is_resource($dh)) {
 				while (($filename = readdir($dh)) !== false) {
 					$tempmtime = $this->filemtime($filename);
 					if ($tempmtime > $mtime) {
diff --git a/lib/files/storage/mappedlocal.php b/lib/files/storage/mappedlocal.php
index fbf1b4ebf966b98af1bc67e79a926c5fb93e74a3..ba5ac4191c5b9b1fc595fb472c2c17125bd0a2d4 100644
--- a/lib/files/storage/mappedlocal.php
+++ b/lib/files/storage/mappedlocal.php
@@ -65,16 +65,18 @@ class MappedLocal extends \OC\Files\Storage\Common{
 
 		$logicalPath = $this->mapper->physicalToLogic($physicalPath);
 		$dh = opendir($physicalPath);
-		while (($file = readdir($dh)) !== false) {
-			if ($file === '.' or $file === '..') {
-				continue;
-			}
+		if(is_resource($dh)) {
+			while (($file = readdir($dh)) !== false) {
+				if ($file === '.' or $file === '..') {
+					continue;
+				}
 
-			$logicalFilePath = $this->mapper->physicalToLogic($physicalPath.'/'.$file);
+				$logicalFilePath = $this->mapper->physicalToLogic($physicalPath.'/'.$file);
 
-			$file= $this->mapper->stripRootFolder($logicalFilePath, $logicalPath);
-			$file = $this->stripLeading($file);
-			$files[]= $file;
+				$file= $this->mapper->stripRootFolder($logicalFilePath, $logicalPath);
+				$file = $this->stripLeading($file);
+				$files[]= $file;
+			}
 		}
 
 		\OC\Files\Stream\Dir::register('local-win32'.$path, $files);
diff --git a/lib/helper.php b/lib/helper.php
index 5fb8fed3459570aa43ce5688d72d90d86f671e3c..0af7f6f0394a1c11ec0023d37ea94807229372d6 100644
--- a/lib/helper.php
+++ b/lib/helper.php
@@ -341,17 +341,19 @@ class OC_Helper {
 		if (!is_dir($path))
 			return chmod($path, $filemode);
 		$dh = opendir($path);
-		while (($file = readdir($dh)) !== false) {
-			if ($file != '.' && $file != '..') {
-				$fullpath = $path . '/' . $file;
-				if (is_link($fullpath))
-					return false;
-				elseif (!is_dir($fullpath) && !@chmod($fullpath, $filemode))
-					return false; elseif (!self::chmodr($fullpath, $filemode))
-					return false;
+		if(is_resource($dh)) {
+			while (($file = readdir($dh)) !== false) {
+				if ($file != '.' && $file != '..') {
+					$fullpath = $path . '/' . $file;
+					if (is_link($fullpath))
+						return false;
+					elseif (!is_dir($fullpath) && !@chmod($fullpath, $filemode))
+						return false; elseif (!self::chmodr($fullpath, $filemode))
+						return false;
+				}
 			}
+			closedir($dh);
 		}
-		closedir($dh);
 		if (@chmod($path, $filemode))
 			return true;
 		else
@@ -649,9 +651,11 @@ class OC_Helper {
 			// if oc-noclean is empty delete it
 			$isTmpDirNoCleanEmpty = true;
 			$tmpDirNoClean = opendir($tmpDirNoCleanName);
-			while (false !== ($file = readdir($tmpDirNoClean))) {
-				if (!\OC\Files\Filesystem::isIgnoredDir($file)) {
-					$isTmpDirNoCleanEmpty = false;
+			if(is_resource($tmpDirNoClean)) {
+				while (false !== ($file = readdir($tmpDirNoClean))) {
+					if (!\OC\Files\Filesystem::isIgnoredDir($file)) {
+						$isTmpDirNoCleanEmpty = false;
+					}
 				}
 			}
 			if ($isTmpDirNoCleanEmpty) {
@@ -694,7 +698,7 @@ class OC_Helper {
 		$newpath = $path . '/' . $filename;
 		if ($view->file_exists($newpath)) {
 			if (preg_match_all('/\((\d+)\)/', $name, $matches, PREG_OFFSET_CAPTURE)) {
-				//Replace the last "(number)" with "(number+1)" 
+				//Replace the last "(number)" with "(number+1)"
 				$last_match = count($matches[0]) - 1;
 				$counter = $matches[1][$last_match][0] + 1;
 				$offset = $matches[0][$last_match][1];
@@ -705,7 +709,7 @@ class OC_Helper {
 			}
 			do {
 				if ($offset) {
-					//Replace the last "(number)" with "(number+1)" 
+					//Replace the last "(number)" with "(number+1)"
 					$newname = substr_replace($name, '(' . $counter . ')', $offset, $match_length);
 				} else {
 					$newname = $name . ' (' . $counter . ')';
diff --git a/lib/migration/content.php b/lib/migration/content.php
index 2d8268a1d74b2a08fbcd15cea3d25ff18e20fc34..4413d722731db4666d344c6b20cf9e9671fae073 100644
--- a/lib/migration/content.php
+++ b/lib/migration/content.php
@@ -191,7 +191,8 @@ class OC_Migration_Content{
 		if( !file_exists( $dir ) ) {
 			return false;
 		}
-		if ($dirhandle = opendir($dir)) {
+		$dirhandle = opendir($dir);
+		if(is_resource($dirhandle)) {
 			while (false !== ( $file = readdir($dirhandle))) {
 
 				if (( $file != '.' ) && ( $file != '..' )) {