diff --git a/ftpd.py b/ftpd.py
index 046641da9d82b93cce31a98f6d9cd548a3d021e0..c3d67aac713a3d9f026ce4f00a9162e665fb1f7d 100644
--- a/ftpd.py
+++ b/ftpd.py
@@ -13,12 +13,13 @@ def on_file_received(handler, path):
 	elif path.startswith(RAW_TMP):
 		path = path[len(RAW_TMP):].lstrip('/')
 		num = 0
-		suffix = ''
-		while os.path.exists(RAW_TARGET+'/'+path+suffix):
+		rpath = path
+		while os.path.exists(RAW_TARGET+'/'+rpath):
 			num += 1
-			suffix = '.'+str(num)
-		os.rename(RAW_TMP+'/'+path, RAW_TARGET+'/'+path+suffix)
-		requests.get(API_BASEURL+'/internal/sort/autoencode', params={'apikey': API_APIKEY, 'path': path+suffix})
+			p = path.rsplit('.', 1)
+			rpath = '.'.join([p[0]+'_'+str(num)]+p[1:])
+		os.rename(RAW_TMP+'/'+path, RAW_TARGET+'/'+rpath)
+		requests.get(API_BASEURL+'/internal/sort/autoencode', params={'apikey': API_APIKEY, 'path': rpath})
 	else:
 		print("Unknown path:", path)