It looks like your file name update code may not be working properly. (I’m not 100% sure if it’s my environment or your code.)
Basically line 144 in your code, upload.php is writing the correct, new meta value to the database. However, the new file name is not being displayed in the media library. (The old file name is still displayed after replacing the old image with a new image).
Fyi when I comment out line 73…
// $current_filename = basename($current_file);
….everything works fine and the new file name is correctly displayed in the media library post.