[GH-ISSUE #138] prevent overwriting if uploaded file already exists #101

Closed
opened 2026-03-02 15:55:47 +03:00 by kerem · 3 comments
Owner

Originally created by @crispyduck00 on GitHub (Mar 12, 2019).
Original GitHub issue: https://github.com/prasathmani/tinyfilemanager/issues/138

When a file is uploaded which name already exists, the existing file is overwritten without any warning.

To prevent overwriting existing files a (n) can be added to the filename of the newly uplodad file. Where n is a ongoing number starting with 0.

So for example if "file.zip" exists already in the upload path, rename the newly uploaded "file.zip" to "file (0).zip". If again the same filename is uploaded, rename it to "file (1).zip and so on.

This could be easylie archifed by adding following code bevore the "move_upload_file()" function:

     if(file_exists ($fullPath)) {
          $newPath = preg_replace('/\\.[^.\\s]{3,4}$/', '', $fullPath) . ' (0).' . $ext;    	
          $n = 1;
          while(file_exists ($newPath)) {
                $newPath = preg_replace('/\\.[^.\\s]{3,4}$/', '', $fullPath) . ' (' . $n . ').' . $ext;
                $n++;
          }
          $fullPath = $newPath;
    }

regards,
crispyduck

Originally created by @crispyduck00 on GitHub (Mar 12, 2019). Original GitHub issue: https://github.com/prasathmani/tinyfilemanager/issues/138 When a file is uploaded which name already exists, the existing file is overwritten without any warning. To prevent overwriting existing files a (n) can be added to the filename of the newly uplodad file. Where n is a ongoing number starting with 0. So for example if "file.zip" exists already in the upload path, rename the newly uploaded "file.zip" to "file (0).zip". If again the same filename is uploaded, rename it to "file (1).zip and so on. This could be easylie archifed by adding following code bevore the "move_upload_file()" function: ``` if(file_exists ($fullPath)) { $newPath = preg_replace('/\\.[^.\\s]{3,4}$/', '', $fullPath) . ' (0).' . $ext; $n = 1; while(file_exists ($newPath)) { $newPath = preg_replace('/\\.[^.\\s]{3,4}$/', '', $fullPath) . ' (' . $n . ').' . $ext; $n++; } $fullPath = $newPath; } ``` regards, crispyduck
kerem 2026-03-02 15:55:47 +03:00
Author
Owner

@prasathmani commented on GitHub (Mar 13, 2019):

@crispyduck00 , added your suggestion. check with latest.

<!-- gh-comment-id:472305064 --> @prasathmani commented on GitHub (Mar 13, 2019): @crispyduck00 , added your suggestion. check with latest.
Author
Owner

@crispyduck00 commented on GitHub (Mar 13, 2019):

@prasathmani , great solution simply adding the date!
Maybe $datetime_format without dot and spaces can be used for it.
What happens if the filename length exceeds the filesystem limit?

Maybe there should be a configurable filename limit for uploads:
like this

Thanks!

<!-- gh-comment-id:472319870 --> @crispyduck00 commented on GitHub (Mar 13, 2019): @prasathmani , great solution simply adding the date! Maybe $datetime_format without dot and spaces can be used for it. What happens if the filename length exceeds the filesystem limit? Maybe there should be a configurable filename limit for uploads: [like this](https://stackoverflow.com/questions/49524701/dropzone-js-check-filename-length-with-accept) Thanks!
Author
Owner

@hedwiggggg commented on GitHub (Mar 14, 2019):

It would be nice, if this feature is configurable; So we can turn it off and it actually overwrites the files.

<!-- gh-comment-id:473064302 --> @hedwiggggg commented on GitHub (Mar 14, 2019): It would be nice, if this feature is configurable; So we can turn it off and it actually overwrites the files.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/tinyfilemanager#101
No description provided.