I've gone back and forth on a few ways of saving multiple image (paths) to the database to be stored and used in a carousel.
I will eventually be doing this in Wordpress, but I have just been testing how the data would work properly.
There will be a number of inputs with the name="image[]", and then each image will be concatenated (although separated by spaces). From there I will insert it into the database.
Then after querying to retrieve the string I will match all URL's and do with them what I want.
So far it works great, and I have tested it on multiple URL paths.
I still have to set up validation, but my question is .... what are the pro's and con's of saving image information to a database like this?
Here is the code:
<form action=<?php echo $PHP_SELF;?> method="post">
<input type="text" name="images[]"/><br/>
<input type="text" name="images[]"/><br/>
<input type="text" name="images[]"/><br/>
<input type="text" name="images[]"/><br/>
<input type="submit" value="submit"/><br/>
</form>
<?php
if($_POST['images']){
foreach($_POST['images'] as $image)
{
if($image){
$image_data .= $image . " ";
}
}
echo $image_data;
// actually insert this into the db
}
$pattern = '/https?\:\/\/[^\" ]+/i';
$result = preg_match_all($pattern, $image_data, $matches, PREG_PATTERN_ORDER);
foreach($matches as $match){
foreach($match as $image){
if(!empty($image)){
echo '<p>Image: ' . $image . '</p>';
// Actually display in some sort of manner that is a image carousel
}
}
}
?>
I don't think saving web-served images to databases is acceptable at all. Think of the overhead it causes to involve the entire webserver/php/database stack just to get a single image delivered. Bottom line: A database is a poor substitute for a file system.