Media upload overwrite confirmation message

One of my customers is asking that when they upload a file to media, it would be nice to show a message if an old file with the same name will be overwritten, to confirm that is the expected action to do and allow the user to cancel if it’s not. I found this very old issue asking for something similar [FeatureRequest]: Warn users when uploading duplicate file names to the media manager · Issue #3124 · octobercms/october · GitHub and also another person asking for a configuration that allows them to disable the possibility to overwrite files Mediafinder automatically override files

Maybe it would be nice to add these functionalities to the next media manager iteration?

1 Like

I think it would be to take it natively. Because in practice, when mediafinder is used, it is a big bug that files are overwritten without warning.

For my part, I added an event that systematically renames an imported file to prevent them from being overwritten. This can help you maybe:

	Event::listen('media.file.upload', function($widget, $filePath, $uploadedFile) {
            
            $mediaLib = MediaLibrary::instance();

            if ($mediaLib->exists($filePath)) {
                $originalName = $uploadedFile->getClientOriginalName();
                $nameSansExtension = pathinfo($originalName, PATHINFO_FILENAME);
                $slugifiedName = Str::slug($nameSansExtension);
                $extension = $uploadedFile->getClientOriginalExtension();

                $baseDir = dirname($filePath);
                $newFileName = $slugifiedName . '.' . $extension;
                $newChemin = $baseDir . DIRECTORY_SEPARATOR . $newFileName;

                while ($mediaLib->exists($newChemin)) {
                    $newFileName = $slugifiedName . '-' . Str::random(3) . '.' . $extension;
                    $newChemin = $baseDir . DIRECTORY_SEPARATOR . $newFileName;
                }
                $mediaLib->moveFile($filePath, $newChemin, true);
            } else {}
	});
2 Likes

Hi @Maria_UtopigStudio, @lightbenin,

Thanks for bringing this up. It is a valid concern… and a bit of a long running issue with the media manager.

We’re planning to implement a fix for this in October CMS v4.1: When uploading files that already exist, users will see a confirmation dialog listing the conflicting files and asking whether they want to replace them. If they decline, the duplicate files will be skipped and only new files will be uploaded.

Hopefully it will help.

1 Like

This is exactly what we need! Thank you.

That’s really great!