Dot NET
Dot NET

Reputation: 4897

How to make sure that files on the server are not overwritten with files of the same name

I've got an asp:FileUpload control to upload images to my server, which is local.

It works well, however I decided to test what would happen if users upload an image which has the same file name as an existing image on the server. The result is that the original image is overwritten by the new one.

I thought of writing some code which would append a number at the end of the filename if the respective file already exists, however I believe that this is a bit flawed as a user may still be able to provide a file name with a number appended, thus still overwriting.

What would be the best approach to solve this problem?

Upvotes: 0

Views: 1200

Answers (4)

Grant Thomas
Grant Thomas

Reputation: 45083

In your database store both a fileName and a friendlyFileName field. Keep track of the original file name in the latter field for display purposes and store a GUID (see the Guid structure of .NET) in the former.

Then, obviously use the unique identifier to name the physical file resulting from the upload.

Depending on the size, nature, rate of access and whatnot, you may store these files in the database itself as binary - whether or not this is a good idea is a debate in and of itself - at least one advantage of doing so is such organisation in a 'stand alone' system.

Upvotes: 1

mishmash
mishmash

Reputation: 4458

Simple answer: Don't rely on the user to provide a correct name.

You should create a unique filename for the file that was just uploaded, then save that file in a table so that you can reference it later.

For example:

User uploads a file named foo.file. When the file is uploaded you create a unique filename for it and store the users name in the database table, so a row in the table would look like:

file_id    |  file_name   |   file_rname
1             39189f9381      foo.file

This way, you don't have to worry about files being overwritten yet you can still present the filename the user choose so there wont be any confusion.

Hope this helps

Upvotes: 1

BrokenGlass
BrokenGlass

Reputation: 160902

Since there is always a chance for duplicate file names and assuming you have to keep track of the file name the user originally chose I would just use a name that is guaranteed to be unique (i.e. a GUID or some sort of primary key) and keep track of the mapping between server assigned file name and user designated file name in the database.

Alternatively if you don't have to keep track of the original file name adding numbers (or "Copy of") will also work - if the file name with the added number already exists, just add another number, and so on. That's what Windows does by default when you copy a file into the same folder it came from.

Upvotes: 0

jrummell
jrummell

Reputation: 43077

You can use a Globally Unique IDentifier to ensure that each file name is unique. If you want to preserve the original file name, you can prefix it with a GUID.

if (File.Exists(fileName))
{
   fileName = Guid.NewGuid() + fileName;
}

Upvotes: 1

Related Questions