0

Can someone please give me advice on how the technique of storing a temp file works.

For example I am creating a simple web Photo Gallery management. I am saving the PhotoGallery and the Photo information in the database using PhotoGalleryId as a foreign key. With each photo I am storing the FileName: i.e. (photoname.jpg). This way when I go and retrieve the photos from the database I can simply combine the filename with the root path to set the img src.

I would like to further enhance this so that when the user uploads the files a random generated filename is used rather then the one they had it names as to ensure it will always be unique. Additionally can someone explain the technique of using a temp directory and if I have to store the temp filename in the database (Photos table).

Blake Rivell
  • 13,105
  • 31
  • 115
  • 231
  • If you know which folder is containing the image you coud completly over go this problem. My suggestion would either be to use a part of the hash of the photoimage or to just randomly create the name with a sufficient filename length and then try to insert it into your db column which holds an unique constraint, and to seperatly store the file extension in a column if you want to preserve the original one, so you could for example do something like this abc.com/img/h5b923ijo and intrnally referring to the file, just an idea :) – prizm1 Aug 21 '16 at 20:09

1 Answers1

1

You could:

1) rename the file before saving to anything (including using a GUID if you want), and store the file data as such:

Create table fileData(...originalFileName varchar(x), savedFileName varchar(x), basePath varchar(x)...)

That will keep track of the original upload file name, the path in which you stored your file, and the file name you decided to save it under. There is no need for temp file.

2) Store the photo itself in the DB and eliminate directories/naming issues entirely.

If you want specifics, show your code.

Shannon Holsinger
  • 2,293
  • 1
  • 15
  • 21
  • Thank you for this response can you answer these couple of questions: So the purpose of storing the original filename is if the user goes to download the file I can give it back to them with the original name otherwise the original filename is useless correct? The img src will always use the Random generated filename because that is the name that is actually on disk correct? Additionally why would I need to store the basePath if it is a system setting so I can just combine it with the fileName. – Blake Rivell Aug 21 '16 at 19:17
  • 1
    Yes - in this case, the original filename wouldn't be needed unless you wanted to give it back to the user - you mentioned saving it so that's why I made my answer like that, but it would basically be worthless. Storing the basepath would allow you to make different directories in case you get a large number of files (different directories per month or per 1,000 files, etc.) They're just ideas - nothing set in stone. – Shannon Holsinger Aug 21 '16 at 20:14