if upload two same name files into media manager, the files uploaded later will auto overwrite the previous file without prompt. what did i do for prevent auto overwrite? i have many files in one directory,about 5000, when i upload new files into media manager, i must check the file name first, but this become more difficulty when the numbers of files getting more and more .
did there have any good solutions?
@buuug7 Just Create a folder then upload the same file into that folder
@korneliuskristianr if you edito a article and add images with richeditor with

it will auto upload image into uploaded-files directory, when you add a second article , also with a same name image, it will overwrite the previours images. when you see your article, first article and second article have a sample image. but people need it different.
want i need do?
if uploaded files generate a random string name , it will prevent this problem. for same image ,user can manager manually, as below, just delete the same images.

I have same problem with the media manager.
I override important file (that was uploaded in past) with newer file with same filename.
None prompt or error or warning was displayed.
I have few questions:
Is there any configuration for change this behaviour to raise error in this case or auto-rename newly uploaded file?
Or is it possible change this behaviour via events/extensions/plugins of MediaManager?
Another way is using my own model with related "File Attachments" ('System\Models\File') instead of MediaManager. There is not problem with overriding existing files, but if user downloads the file then original filename is lost and the filename is 558ab0f0aa231581256800.xls instead.
It is possible download this file with original filename?
Thank you for answers
@tgz-cz i have no best solutions, it seems only by modifying the source code of modules/cms/widgets/MediaManager.php line 1098 ,as follows
protected function checkUploadPostback()
{
$fileName = null;
$quickMode = false;
if (
(!($uniqueId = Request::header('X-OCTOBER-FILEUPLOAD')) || $uniqueId != $this->getId()) &&
(!$quickMode = post('X_OCTOBER_MEDIA_MANAGER_QUICK_UPLOAD'))
) {
return;
}
try {
if (!Input::hasFile('file_data')) {
throw new ApplicationException('File missing from request');
}
$uploadedFile = Input::file('file_data');
// remove this line
$fileName = $uploadedFile->getClientOriginalName();
// add this line
$fileName = str_random(40);
@buuug7 feel free to submit a PR to add a warning popup if the file is going to get overwritten.
@LukeTowers how to determin the two files the same ?
MediaLibarary have a method exists, it just detemines a file with the specified path exists in the library. It not necessary to give the prompt when two files have a same filename, if upload file have the same filename with already updated files. just auto rename the filename. it was simple. that is also work when use richeditor upload file.
source file modules/cms/widgets/MediaManager.php checkUploadPostback() method, add following code.
only throw exception
if(MediaLibrary::instance()->exists($filePath)){
throw new ApplicationException(Lang::get('cms::lang.media.file_exists'));
}
rename file name
if(MediaLibrary::instance()->exists($filePath)){
$fileName = File::name($uploadedFile->getClientOriginalName()) . '-' . str_random(40) . '.' . $extension;
$filePath = $path . '/' . $fileName;
}
which better?
I think the exception is better for now, until we get around to providing a popup that would let the user choose to rename the file on upload.
@LukeTowers Ok
Duplicate of #1117, closing that one and keeping this one open.
@daftspunk can you add some event before file upload or when filename is generating?
@MaxNozhkin there's the after upload event: https://octobercms.com/docs/api/media/file/upload. You could make a PR to propose a beforeUpload event if you'd like
I found a solution to rename and return the renamed file to the richeditor, without change source code MediaManager
public function boot()
{
Event::listen('media.file.upload', function($widget, $filePath, $uploadedFile) {
$originalName = $uploadedFile->getClientOriginalName();
$user = BackendAuth::getUser();
$sanitizedFileName = Carbon::now()->format('dmYHis') . $user->login . '.' . $uploadedFile->getClientOriginalExtension();
$filePathChunks = explode(DIRECTORY_SEPARATOR, $filePath);
$filePathChunks[(count($filePathChunks) - 1)] = $sanitizedFileName;
$newPath = implode(DIRECTORY_SEPARATOR, $filePathChunks);
$isRename = true;
MediaLibrary::instance()->moveFile($filePath, $newPath, $isRename);
header('Content-Type: application/json');
echo json_encode(['result' => 'success', 'link' => MediaLibrary::url($newPath)]);
exit();
});
}
@pavelmgn submit a PR to pass the $filePath argument as a reference instead,
header('Content-Type: application/json');
echo json_encode(['result' => 'success', 'link' => MediaLibrary::url($newPath)]);
exit();
is a pretty hacky solution that is unnecessarily brittle. I don't think changing the event argument for this case to pass by reference instead is going to cause any breaking changes so submit a PR and I'll merge it in.
@LukeTowers #4612
Sorry, if I did something wrong, the first time I did PR. Did that mean?
This issue will be closed and archived in 3 days, as there has been no activity in the last 30 days. If this issue is still relevant or you would like to see action on it, please respond and we will get the ball rolling.
$filePath a referenced variable, allowing the filePath to be changed via the media.file.upload event and providing a mechanism to prevent duplicate images and files. This will be released with build 460.Note that #4616 does not solve this issue, it just makes it possible for a plugin to solve this issue instead. We can still consider a core solution to this specific issue.
This issue will be closed and archived in 3 days, as there has been no activity in the last 30 days. If this issue is still relevant or you would like to see action on it, please respond and we will get the ball rolling.
This issue will be closed and archived in 3 days, as there has been no activity in the last 30 days. If this issue is still relevant or you would like to see it actioned, please respond and we will re-open this issue.
This issue will be closed and archived in 3 days, as there has been no activity in the last 30 days.
This issue will be closed and archived in 3 days, as there has been no activity in the last 30 days.
If this issue is still relevant or you would like to see it actioned, please respond and we will re-open this issue.
If this issue is critical to your business, consider joining the Premium Support Program where a Service Level Agreement is offered.
What about changing line 1533 and 1540 in /modules/backend/widgets/MediaManager.php
$fileName = $fileNameClean . '.' . $extension;
to
File::name($fileName).date('YmdHis').'.'.$extension;
and
$fileName = $fileNameClean.date('YmdHis').'.'.$extension;
Would be an easy fix and preventing files to be overwritten or is the file being overwritten the desired behavior?
@setianke the problem in my eyes is the current behaviour not giving the user the choice. I personally don't like including the upload date in the filename, so I would prefer that users on uploading a duplicate file were presented with the option to either cancel, replace, or keep both (in which case a (1), (2), etc) gets added to the latest upload's filename.
@LukeTowers agreed, imo an option in the Settings enabling the automatic date in filename on/off or show warning with cancel/replace/keep both would be a good solution (just my 2cents)
This issue will be closed and archived in 3 days, as there has been no activity in the last 30 days.
If this issue is still relevant or you would like to see it actioned, please respond and we will re-open this issue.
If this issue is critical to your business, consider joining the Premium Support Program where a Service Level Agreement is offered.
This issue will be closed and archived in 3 days, as there has been no activity in the last 60 days.
If this issue is still relevant or you would like to see it actioned, please respond and we will re-open this issue.
If this issue is critical to your business, consider joining the Premium Support Program where a Service Level Agreement is offered.
This issue will be closed and archived in 3 days, as there has been no activity in the last 60 days.
If this issue is still relevant or you would like to see it actioned, please respond and we will re-open this issue.
If this issue is critical to your business, consider joining the Premium Support Program where a Service Level Agreement is offered.
This issue will be closed and archived in 3 days, as there has been no activity in the last 60 days.
If this issue is still relevant or you would like to see it actioned, please respond and we will re-open this issue.
If this issue is critical to your business, consider joining the Premium Support Program where a Service Level Agreement is offered.
This issue will be closed and archived in 3 days, as there has been no activity in the last 60 days.
If this issue is still relevant or you would like to see it actioned, please respond and we will re-open this issue.
If this issue is critical to your business, consider joining the Premium Support Program where a Service Level Agreement is offered.
Most helpful comment
@setianke the problem in my eyes is the current behaviour not giving the user the choice. I personally don't like including the upload date in the filename, so I would prefer that users on uploading a duplicate file were presented with the option to either cancel, replace, or keep both (in which case a (1), (2), etc) gets added to the latest upload's filename.