0

So i have an idea how to implement my gallery on my website, but need help from someone experienced to see if it's OK or not.

So basically the idea is that when a moderator uploads a few images to an existing article, i would rename those images in something like [ name of the article + 8 random numbers ] just in case there are no duplicates. When i generate the new name, then the system will save it in a specific folder. And after that I append each name of an image on to an array which I later save in a database single column for that article in question. But each name is divided with a comma.

So my question is, would this be an OK solution? Never done this kind a thing so would really appreciate some sort of guidance.

EDIT 1: Ok the idea goes like this:

Moderator wants to add images to an existing article, he chooses the images, and uploads them for this example lets say there are 5 images. After the upload is completed the PHP code changes the names of the images uploaded to [article_name]+[random number, length 8] after that i plan to put the new names in an array like this:

array(image1,image2,image3,image4,image5);

After that it's only a matter of saving it to the DB and when needed i have the name of the images to be used in the gallery.

EDIT 2: For saving I intend to use the implode function. After saving it later for the gallery I can easily use the explode function to get every name out of the string.

2
  • Use unique ID's instead of random numbers to identify a picture. Commented Mar 1, 2014 at 9:39
  • what have you tried so far or at-least tell us do you got any (little bit) idea how to do this? Commented Mar 1, 2014 at 9:39

1 Answer 1

1

You should not implode the array and save it in one single field in the database. Instead you should create a new table like

CREATE TABLE article_images
  id int(11) PRIMARY KEY AUTO_INCREMENT,
  article_id int(11) INDEX,
  ...
);

This table "links" back to the article-table with the column article_id so you can find out which images belong to which article.

How you save your images is up to you. You can indeed use some approach like you propose. I think I would go a different way like creating folders for every article-id and inside create the images based on their id of the corresponding row in article_images, like

/
 123/
   5.png
 456/
   6.jpg

where 123 and 456 are article-ids and 5 and 6 are ids of rows in article_images. But this is just my proposal. There exist lots of different ways, which are all more or less good and secure.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok so your first proposal is to create a separate table to store each image name/path separately? And the folder proposal is not that bad of an option.
Yes, this is called "normalization". You can take a (brief) look at for example this: en.wikipedia.org/wiki/Database_normalization

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.