diff --git a/lib/files/view.php b/lib/files/view.php
index 968b755a66185226a2768568a19da6432dc4df80..aa08a5f7cc9f2ad48ec756068283281850ff6d22 100644
--- a/lib/files/view.php
+++ b/lib/files/view.php
@@ -500,7 +500,7 @@ class View {
 				} else {
 					if ($this->is_dir($path1) && ($dh = $this->opendir($path1))) {
 						$result = $this->mkdir($path2);
-						if(is_resource($dh)) {
+						if (is_resource($dh)) {
 							while (($file = readdir($dh)) !== false) {
 								if (!Filesystem::isIgnoredDir($file)) {
 									$result = $this->copy($path1 . '/' . $file, $path2 . '/' . $file);
@@ -975,7 +975,7 @@ class View {
 	/**
 	 * search for files by mimetype
 	 *
-	 * @param string $query
+	 * @param string $mimetype
 	 * @return array
 	 */
 	public function searchByMime($mimetype) {
@@ -998,7 +998,7 @@ class View {
 
 			$results = $cache->$method($query);
 			foreach ($results as $result) {
-				if (substr($mountPoint . $result['path'], 0, $rootLength) === $this->fakeRoot) {
+				if (substr($mountPoint . $result['path'], 0, $rootLength + 1) === $this->fakeRoot . '/') {
 					$result['path'] = substr($mountPoint . $result['path'], $rootLength);
 					$files[] = $result;
 				}
@@ -1012,9 +1012,11 @@ class View {
 
 					$relativeMountPoint = substr($mountPoint, $rootLength);
 					$results = $cache->$method($query);
-					foreach ($results as $result) {
-						$result['path'] = $relativeMountPoint . $result['path'];
-						$files[] = $result;
+					if ($results) {
+						foreach ($results as $result) {
+							$result['path'] = $relativeMountPoint . $result['path'];
+							$files[] = $result;
+						}
 					}
 				}
 			}
diff --git a/tests/lib/files/view.php b/tests/lib/files/view.php
index 0de436f570a5ddbc35b0ecf37293a50ef98fd408..3043f132b73ea118af1e7bcd1f6e4f752c613f70 100644
--- a/tests/lib/files/view.php
+++ b/tests/lib/files/view.php
@@ -354,8 +354,22 @@ class View extends \PHPUnit_Framework_TestCase {
 		$this->hookPath = $params['path'];
 	}
 
+	public function testSearchNotOutsideView() {
+		$storage1 = $this->getTestStorage();
+		\OC\Files\Filesystem::mount($storage1, array(), '/');
+		$storage1->rename('folder', 'foo');
+		$scanner = $storage1->getScanner();
+		$scanner->scan('');
+
+		$view = new \OC\Files\View('/foo');
+
+		$result = $view->search('.txt');
+		$this->assertCount(1, $result);
+	}
+
 	/**
 	 * @param bool $scan
+	 * @param string $class
 	 * @return \OC\Files\Storage\Storage
 	 */
 	private function getTestStorage($scan = true, $class = '\OC\Files\Storage\Temporary') {