diff --git a/apps/files_external/3rdparty/smb4php/smb.php b/apps/files_external/3rdparty/smb4php/smb.php
index 9650f809041f687c33fd2138bb74a5581613ef66..87638271f0ecc441cb4aa9d6fd3a82119533f979 100644
--- a/apps/files_external/3rdparty/smb4php/smb.php
+++ b/apps/files_external/3rdparty/smb4php/smb.php
@@ -302,6 +302,7 @@ class smb {
 	}
 
 	function rename ($url_from, $url_to) {
+		$replace = false;
 		list ($from, $to) = array (smb::parse_url($url_from), smb::parse_url($url_to));
 		if ($from['host'] <> $to['host'] ||
 			$from['share'] <> $to['share'] ||
@@ -314,7 +315,20 @@ class smb {
 			trigger_error('rename(): error in URL', E_USER_ERROR);
 		}
 		smb::clearstatcache ($url_from);
-		$result = smb::execute ('rename "'.$from['path'].'" "'.$to['path'].'"', $to);
+		$cmd = '';
+		// check if target file exists
+		if (smb::url_stat($url_to)) {
+			// delete target file first
+			$cmd = 'del "' . $to['path'] . '"; ';
+			$replace = true;
+		}
+		$cmd .= 'rename "' . $from['path'] . '" "' . $to['path'] . '"';
+		$result = smb::execute($cmd, $to);
+		if ($replace) {
+			// clear again, else the cache will return the info
+			// from the old file
+			smb::clearstatcache ($url_to);
+		}
 		return $result !== false;
 	}
 
diff --git a/apps/files_external/lib/streamwrapper.php b/apps/files_external/lib/streamwrapper.php
index aa42cbde828083f24f2057f56ef29a8bf0069b01..7a1991d4f04fc7ef136733d0905fe2839446584c 100644
--- a/apps/files_external/lib/streamwrapper.php
+++ b/apps/files_external/lib/streamwrapper.php
@@ -38,7 +38,7 @@ abstract class StreamWrapper extends Common {
 	}
 
 	public function filetype($path) {
-		return filetype($this->constructUrl($path));
+		return @filetype($this->constructUrl($path));
 	}
 
 	public function file_exists($path) {
diff --git a/tests/lib/files/storage/storage.php b/tests/lib/files/storage/storage.php
index 5b5b85568594f7a0c86a2b616334e686c0eb2fdd..19113f52623345f57d21b1b8540a425d01dd2584 100644
--- a/tests/lib/files/storage/storage.php
+++ b/tests/lib/files/storage/storage.php
@@ -139,7 +139,15 @@ abstract class Storage extends \PHPUnit_Framework_TestCase {
 		$this->instance->rename('/source.txt', '/target2.txt');
 		$this->assertTrue($this->instance->file_exists('/target2.txt'));
 		$this->assertFalse($this->instance->file_exists('/source.txt'));
-		$this->assertEquals(file_get_contents($textFile), $this->instance->file_get_contents('/target.txt'));
+		$this->assertEquals(file_get_contents($textFile), $this->instance->file_get_contents('/target2.txt'));
+
+		// move to overwrite
+		$testContents = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
+		$this->instance->file_put_contents('/target3.txt', $testContents);
+		$this->instance->rename('/target2.txt', '/target3.txt');
+		$this->assertTrue($this->instance->file_exists('/target3.txt'));
+		$this->assertFalse($this->instance->file_exists('/target2.txt'));
+		$this->assertEquals(file_get_contents($textFile), $this->instance->file_get_contents('/target3.txt'));
 	}
 
 	public function testLocal() {