Pregunta

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?
¿Fue útil?

Solución

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

Licenciado bajo: CC-BY-SA con atribución
scroll top