Forum Moderators: coopster

Message Too Old, No Replies

Creating directory and names for uploaded files

Best method to name files and create a directory structure

         

sundaridevi

2:05 pm on Mar 7, 2011 (gmt 0)

10+ Year Member



I'm designing a file upload system and I want to find a good way to assign a random filename to uploaded images as well as have a good way to spread '000s of uploaded files across several directories to speed up access.

Previously I used a naming convention like userid_imgnum.jpg but that's too easy to guess and I don't want anybody to be able to directly access the files. Also I'm looking for a good directory system to store the uploaded files. I've seen recommendations to create directories like:

images/a/ : filenames beginning with 'a'
images/b/ : filenames beginning with 'b'
etc

But I suppose there may be a better way?

jspeed

4:18 pm on Mar 7, 2011 (gmt 0)

10+ Year Member



If your just looking for a random file name you could hash it:

$file_name = md5(rand()*10000000) . ".jpg";

Also, you could store the names of the folders in a table and randomly select one to upload the image to.

sundaridevi

8:52 pm on Mar 7, 2011 (gmt 0)

10+ Year Member



Thanks, I've been looking at the md5 solution, i guess i'll just do that.

I like your directory solution.