1

I'm developing a website (using PHP, JS, and MYSQL) which allow user to upload images. My requirements are as below:

  1. User is able to upload 1 or multiple images at a time.
  2. Website is able to save the original images and using the original images to create a re-sized version e.g. 500x500 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 :)

  1. I did not store the images in database as blobs.
  2. 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.
  3. 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.
  4. Is my approach correct? I am thinking about JSON but is it useful in this case?
2

I did not store the images in database as blobs.

Ok

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.

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 :

  • generating a uuid for each of your image and use it as identifier if you all the image are public
  • check access right to the image depending on who is connected.

Is my approach correct? I am thinking about JSON but is it useful in this case?

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 ?,...)

  • First of all thank you for your valuable feedback. Actually.... for each user registered, I created a folder in my filesystem XD (but inside the folder is blank). After that, when they upload any images, I will capture the time in the format YYYYMMDDHHMISS e.g. 20160812170710 and make a folder with this name. This name will also stored in my database as image path. Therefore I am able to map the image path (in db) and the filesystem path~ – 4 Leave Cover Aug 12 '16 at 9:08
  • 1
    hum so if the user upload multiple image at the same time, they may be created in the same second right ? You should add milliseconds :p – Walfrat Aug 12 '16 at 9:10
  • Technically you are right! I would like to share why I did this, for example.. Within 1 seconds user uploaded 4 images, program will capture and insert data into db 1 by 1.. therefore, I am able to know at that particular seconds, in which sequence the images being uploaded and created. This is why I stop at seconds instead of milliseconds as the folder name... – 4 Leave Cover Aug 12 '16 at 9:22
  • 1
    @4LeaveCover Or just name the images by their Id in the database.. – Alternatex Aug 12 '16 at 10:49
  • 1
    @4LeaveCover: Alternatex is right, the better approach is probably to use unique IDs as file names for the images, which avoids collisions totally. Store the original name instead in the database table (together with any other metadata you might need, for example name of uploader, time of uploading, description provided). That way, you do not have to deal with problematic file names your file system might not support, or similar quirks. Using different folders (maybe per day or hour) is still a good idea, to avoid file system restrictions concerning no of files per folder. – Doc Brown Aug 12 '16 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.