random_str() generates a random string, but this does not mean it won't spit the same thing out twice. It's unlikely to collide but if you want to be more sure consider phps uniqid() function instead: {timestamp}_uniqid()
Image Storing Folder Structure
I have made a post form, that STOREs a new post. After the post is made the user can then SHOW the post and add images.
The way I setup the images folders is like so {post_id}/{timestamp}_{random_str(4)}. This worked just fine to stop any clashes that may happen as I didn't want to ever delete an image that was uploaded, just remove it from the database.
The problem I now face is that I have been instructed to now add the images in the same form as creating the post. Therefore I can no longer use ~~{post_id}~~. As the post has not been saved yet and the images are auto uploaded via ajax, using dropzone.js.
So I kind of have two ideas in my head of how to do it, but am unsure if either is the best option and was hoping the community could give me some feedback on the best way to do this.
Option 1: {user_id}/{timestamp}_{random_str(4)}
Option 2: {timestamp}_{random_str(4)}
I would prefer something like option 2, but I am worried that clashes may occur? Is this possible?
I might personally look at giving each image a UUID for the name when storing, and store the images in one folder. The likely hood of a UUID clashing is very very slim. Ben Ramsey's UUID package makes it dead simple to ( ramsey/uuid).
Structure wise I might have the folders laid out as such.
storage/public/posts/images --- with all post images going in that one folder. You can easily append the post ID to a uuid ( you would need to use an underscore or another separator).
Just my two cents
Please or to participate in this conversation.