If I want to link content in a database to a file what can I use to uniquely reference the file in the database other than the file or the creation/modification date name which might change. Is there something else that is unique to a file that will not change and can be used to reference the file in a database?
I want to reference db content data that corresponds to an m3u8 media file, so the when I play the file the db content is retrieved and displayed. I want to be able to modify the m3u8 file name and change its content so I need to reference the file using something other than filename or mtime.
I usually fix this with time()
, because there is almost zero possibility to have a duplicate entry of time()
(unless by some miracle two file processing happens at the same fraction of time).
I don't stop there, I sometimes add the username to the string, along with some other random code that makes it almost duplicate-proof (the possibility is near zero).
Lets say a file name is super-awersome-pdf-file.pdf
. I add time()
(which is a string) in front of it, then some other stuff. That filename itself becomes an unique id. Furthermore, I also store the info in a Database table to give it another unique ID. Therefore, I have two things to work with.
By keeping the filename to less than 60-70 chars, you can make some pretty unique filenames this way. Then you can add information about it to the Database.
Pro: Nearly never you will get a duplicate filename
Con: however this may create duplicate files since the system will see that the two files have different file names.
So, no duplicate filename but probably if the user is not careful some duplicate files.