Take the 2-minute tour ×

Is there a "standard" way of storing photos on the web, when working with photo galleries? Is it bad practice to store photos in a mysql database? Should I store the photo in a folder, and store the link in a database?

What is "good practice" when talking about storing photos, to use on a website?

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

You can store photos in your database using the different BLOB data types. If the photos are excessive in quantity, or extra large, the database my start to take a performance hit and will increase in size drastically. Essentially, if you have 100 10Mb photos, that's 1,000Mb or around a Gb of size that the database will grow. Backups will take forever to execute, and if a table containing the images corrupts for whatever reason, it'll induce more headache than you'll care to deal with.

My suggestion as imoda recommends is to store them on a hard drive, and link to them in the database where you'll hold all of the metadata and a link to the location of the file. Best practice is to store the photo's file name in the database, and rename the file to use the primary key's value as the file name As an example, row 5 associates to photo "x" where x is the filename, store it on the hard drive and rename the photo to the file name of 5, and then when you go to access it for things like download, you'll pull the actual file name from row 5 in the database and rename it before you download it. That'll keep from accidentally overwriting the file if you happen to upload two or more files with the same file name.

share|improve this answer
Well, they are not for downloading, but I get the point. I was just wondering, what is "best practice" since I'm new to photo galleries :) Thanks for your answer –  hogni89 Jun 25 '11 at 0:32
Your welcome. There's a giant debate today with regards to whether or not BLOBS should be used, with strong arguments on both sides, but it has to be determined when put into context. One thing I did not mention is doing checks to ensure that files do not go missing off the file server without having their records deleted from the database. Shouldn't happen, but it can. Might suggest setting up an automated task to check every so often to ensure the documents still exist and take action if they don't (obviously an argument the pro-BLOB side would use). –  Scott Jun 25 '11 at 0:57
add comment

I suggest to use Flickr to add/Remove et rearrange your picture. The advantage of using Flickr is that you don't have to write any complex code or host the pictures. You can use PHP and the Flickr API to retrieve the pictures using your profile ID. 1. Create an App with Flickr 2. Download phpFlickr and put it on the root of your server https://code.google.com/p/phpflickr/downloads/list add this PHP code to your gallery.php file

Here is the code for the PHP Gallery with Pagination

<div class="portfolio-grid">


        $key = "92d657c8dc79a29108d846a5fd121a29";  //your API key
        $nsid = "108400461@N02";            //your NSID


        // inclue the core file
        // Fire up the main phpFlickr class 
        $f = new phpFlickr($key);

        $photos = $f->people_getPublicPhotos($nsid, NULL, NULL, 20, $page);

    <!--END script Flick photos -->
            echo '<div id="container">';
            // loop through each photo

                 echo '<div class="item" style="float:left;">
                        <a class="swipebox-isotope" href="' . $f->buildPhotoURL($photo, 'Large') . '" title="A.S.A.C - The African Student&apos;s Association of Concordia"><img class="gallery-img" src="' . $f->buildPhotoURL($photo, 'medium') . '" alt="' . $photo['title'] . '" title="' . $photo['title'] . '"/></a>

            // end loop
            echo '</div>';


  <h2 id="nav">
        $back = $page - 1;
        $next = $page + 1;
        // if it's not the first page
        if($page > 1) {

        // if not last page
        if($page != $pages) {
        echo "<a style='float:right' href='?page=$next'><strong>Next</strong> &raquo;</a>";}
        echo"<h3 style='text-align:center;'>Page $page of $pages <br/>$total photos</h3>";

share|improve this answer
add comment

99% of the time you don't want to store a photo in a MySQL database. It's incredibly resource intensive if done improperly. Store your photos in directories on your website and store the path to them in your database.

share|improve this answer
add comment

Your Answer


