I'm developing a website (using PHP, JS, and MYSQL) which allow user to upload images. My requirements are as below:
- User is able to upload 1 or multiple images at a time.
- Website is able to save the original images and using the original images to create a re-sized version e.g. 500×500 px
What I've done
I need some opinions regarding the decisions I've took/implemented as below, let me know if it is right or wrong 🙂
- I did not store the images in database as blobs.
- I have an image table in the database which store image name and image path. The image path is a string which will be the exact folder name in the filesystem, the image name is the name of the image uploaded by user. Combine both will create a path direct to the file system.
- Using above method I am able to query the images as if it was stored in my database but actually I am just mapping them.
- Is my approach correct? I am thinking about JSON but is it useful in this case?
Best Answer
Ok
So if 2 users upload a mommy.png you will have a conflict isn't it ?
Usually in order to not have any conflict and to not have huge performance loss when having a huge amount of image in the same folder (it depends on the type of filesystem) you create intermediary folder which name correspond to some hash.
Exemple : I have a system which compute a MD5 on my file which is like '123456789ab...' it will be stored under 12/34/56/123456789ab'.
In order to retrieve the fill i have a dedicated url which look like http://.../image/1.
Note using database id expose your website to be easy to harvest consider either :
Nop JSON isn't made to transfer files.
If the approach i suggested is a bit complicated consider at least the following : create one directory at least per user and store their image in. If the images aren't public, don't map
http://.../image/<user>/<image>
so easily. Map it to some code where you can perform right check (is user == currentUser ? , is user in friendlist of the image's owner ?,...)