1

I am going to build a PHP photo gallery from scratch but I am trying to determine the best way of doing this. I plan on uploading all photos into a single folder there will not be sub folders because I don't need separate albums created.

I have come up with two different ways of getting the pictures to display. My first method would be to store all of the picture names in a MySQL database and just use a SELECT * FROM pictures and just have it loop through and echo out the HTML <img> tags for each picture.

Another method would be to use just PHP to get a listing of all files in the picture directory and have it loop through doing the same echo HTML <img> tags like mentioned above.

Is there any advantages or disadvantages to using either method? Maybe someone has a better solution I am just making one from scratch for learning purposes.

Yamaha32088
  • 4,125
  • 9
  • 46
  • 97
  • 1
    A single directory will begin to slow things down when you reach a threshold number of image files because the directory on disk will span nodes requiring multiple nodes to be read; it's better to keep relatively few files in larger numbers of directories than have a single directory with a large number of files – Mark Baker Sep 08 '13 at 16:07
  • 2
    For learning purposes it's often better to read other persons code first, then writing your own. Start with installing an existing solution and understand how it works. You can even install different ones and then look for advantages and disadvantages directly without asking others but instead making your own impressions. – hakre Sep 08 '13 at 16:15

3 Answers3

2

If you are doing this for only learning purposes and using some 100ish photographs, I'd say you should start with storing data in a directory and display them. This will allow you to focus on presentation layouts more. You can build a class to extract the images.

Once your layout looks great, you can expand your database to store your images, write a script to migrate pictures to the database table(s) and extend the class to extract images so that they come from the database instead of the file system. This will engage your mind in abstracting layers of operations, making database changes and ...version controlling your changes (I recommend git).

That way you can simulate a client who had few needs at first (pictures on the file system) and their needs grew (pictures in the database), and how you would manage the change in real life. Going one step forward, you could version control your journey on GitHub. That call allow StackOverflow users to fork your repository and help you make changes to your code.

zedfoxus
  • 35,121
  • 5
  • 64
  • 63
  • 1
    These are good suggestions. If I may add one: create an abstraction layer which transparently handles displaying the images. Then, you can simply interchange two classes (one for the file system, one for the database). – ComFreek Sep 08 '13 at 16:33
1

I would suggest to store all names in a database!

Advantages:

  • It allows building relations if you later plan to create other tables.

  • It (directly) allows sorting

  • It (directly) allows filtering

Disadvantages:

  • You have to synchronize the file names and the table if you edit or remove from one of them.

You could also store the images in your database. Whether this option is recommendable depends on your use case and amount of visitors. See here for further information on that topic: PHP to store images in MySQL or not?

Community
  • 1
  • 1
ComFreek
  • 29,044
  • 18
  • 104
  • 156
1

I would use the database method. If you ever decide to add a title, description, tags or comments to each picture you'll have an easier time implementing it. Plus you can sort.

SeanWM
  • 16,789
  • 7
  • 51
  • 83