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?
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.
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.
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">
<?php
$key = "92d657c8dc79a29108d846a5fd121a29"; //your API key
$nsid = "108400461@N02"; //your NSID
// get page number from the url - if there isn't one - we're on page 1
$page = isset($_GET['page']) ? $_GET['page'] : 1;
// inclue the core file
require_once('phpFlickr/phpFlickr.php');
// Fire up the main phpFlickr class
$f = new phpFlickr($key);
$f->enableCache("fs", "cache");
$photos = $f->people_getPublicPhotos($nsid, NULL, NULL, 20, $page);
$pages = $photos[photos][pages]; // returns total number of pages
$total = $photos[photos][total]; // returns how many photos there are in total
?>
<!--END script Flick photos -->
<div>
<?php
echo '<div id="container">';
// loop through each photo
foreach ($photos['photos']['photo'] as $photo) {
// print out a link to the photo page, attaching the id of the 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's Association of Concordia"><img class="gallery-img" src="' . $f->buildPhotoURL($photo, 'medium') . '" alt="' . $photo['title'] . '" title="' . $photo['title'] . '"/></a>
</div>';
// end loop
}
echo '</div>';
?>
</div>
</div>
<h2 id="nav">
<?php
// Some simple paging code to add Prev/Next to scroll through the thumbnails
$back = $page - 1;
$next = $page + 1;
// if it's not the first page
if($page > 1) {
echo "<a href='?page=$back'>« <strong>Prev</strong></a>";
}
// if not last page
if($page != $pages) {
echo "<a style='float:right' href='?page=$next'><strong>Next</strong> »</a>";}
// a quick bit of info about where we are in the gallery
echo"<h3 style='text-align:center;'>Page $page of $pages <br/>$total photos</h3>";
?>
</h2>
精彩评论