diff --git a/files/ajax/upload.php b/files/ajax/upload.php
index af7a7acf70292521ec176e22dd272ba1bbb91055..76ea65fe933a03c8058549c03be4e22cefba5719 100644
--- a/files/ajax/upload.php
+++ b/files/ajax/upload.php
@@ -47,7 +47,8 @@ $result=array();
 if(strpos($dir,'..') === false){
 	$fileCount=count($files['name']);
 	for($i=0;$i<$fileCount;$i++){
-		$target=stripslashes($dir) . $files['name'][$i];
+		// $target=stripslashes($dir) . $files['name'][$i];
+        $target = OC_Helper::buildNotExistingFileName(stripslashes($dir), $files['name'][$i]);
 		if(is_uploaded_file($files['tmp_name'][$i]) and OC_Filesystem::fromTmpFile($files['tmp_name'][$i],$target)){
 			$meta=OC_FileCache::getCached($target);
 			$result[]=array( "status" => "success", 'mime'=>$meta['mimetype'],'size'=>$meta['size'],'name'=>$files['name'][$i]);
diff --git a/lib/helper.php b/lib/helper.php
index c33db5f10fe3cd8a1f75951c33603b8f93973026..412f0e6b7643377493c8cf5209d9eaba0b4fb9f8 100755
--- a/lib/helper.php
+++ b/lib/helper.php
@@ -492,4 +492,32 @@ class OC_Helper {
 			}
 		}
 	}
+
+    /**
+     * Adds a suffix to the name in case the file exists
+     *
+     * @param $path
+     * @param $filename
+     * @return string
+     */
+    public static function buildNotExistingFileName($path, $filename)
+    {
+        if ($pos = strrpos($filename, '.')) {
+            $name = substr($filename, 0, $pos);
+            $ext = substr($filename, $pos);
+        } else {
+            $name = $filename;
+        }
+
+        $newpath = $path . '/' . $filename;
+        $newname = $filename;
+        $counter = 2;
+        while (OC_Filesystem::file_exists($newpath)) {
+            $newname = $name . ' (' . $counter . ')' . $ext;
+            $newpath = $path . '/' . $newname;
+            $counter++;
+        }
+
+        return $newname;
+    }
 }