PHP MYSQL example image gallery (blob-storage)
PHP MYSQL example image gallery (blob-storage)
Image gallery using php and mysql blob storage was already discussed in this website. Please take a look at it.
Php mysql example image gallery blob storage
Now we are about to see a new tutorial on how to display the data or the image from mysql. This can basically be called as a 2nd version of the previous tutorial. We have updated the code with some extra stuffs.
I have fixed minor bugs in the code and also added some extra code on how to retrieve or show the images from the database on the webpage.
Create a mysql database with the name mydata_base and run this query.
CREATE TABLE `My_new_blob_gallery` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(64) character SET utf8 NOT NULL, `ext` varchar(8) character SET utf8 NOT NULL, `image_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `data` mediumblob NOT NULL, PRIMARY KEY (`id`) );
The php code looks like this create a file named blob_version2.php and update it with the code below.
<?php //please donot remove this line : Tutorial created by mistonline.in version 2 of blob storage and displaying the image. $db_host = 'localhost'; // don't forget to change $db_user = ''; $db_pwd = ''; $database = 'mydata_base'; $table = 'My_new_blob_gallery'; // use the same name as SQL table $password = '123'; // simple upload restriction, // to disallow uploading to everyone if (!mysql_connect($db_host, $db_user, $db_pwd)) die("Can't connect to database"); if (!mysql_select_db($database)) die("Can't select database"); // This function makes usage of // $_GET, $_POST, etc... variables // completly safe in SQL queries function sql_safe($s) { if (get_magic_quotes_gpc()) $s = stripslashes($s); return mysql_real_escape_string($s); } // If user pressed submit in one of the forms if ($_SERVER['REQUEST_METHOD'] == 'POST') { // cleaning title field $title = trim(sql_safe($_POST['title'])); if ($title == '') // if title is not set $title = '(empty title)';// use (empty title) string if ($_POST['password'] != $password) // cheking passwors $msg = 'Error: wrong upload password'; else { if (isset($_FILES['photo'])) { @list(, , $imtype, ) = getimagesize($_FILES['photo']['tmp_name']); // Get image type. // We use @ to omit errors if ($imtype == 3) // cheking image type $ext="png"; // to use it later in HTTP headers elseif ($imtype == 2) $ext="jpeg"; elseif ($imtype == 1) $ext="gif"; else $msg = 'Error: unknown file format'; if (!isset($msg)) // If there was no error { $data = file_get_contents($_FILES['photo']['tmp_name']); $data = mysql_real_escape_string($data); // Preparing data to be used in MySQL query mysql_query("INSERT INTO {$table} SET ext='$ext', title='$title', data='$data'"); $msg = 'Success: image uploaded'; } } elseif (isset($_GET['title'])) // isset(..title) needed $msg = 'Error: file not loaded';// to make sure we've using // upload form, not form // for deletion if (isset($_POST['del'])) // If used selected some photo to delete { // in 'uploaded images form'; $id = intval($_POST['del']); mysql_query("DELETE FROM {$table} WHERE id=$id"); $msg = 'Photo deleted'; } } } elseif (isset($_GET['show'])) { $id = intval($_GET['show']); $result = mysql_query("SELECT ext, UNIX_TIMESTAMP(image_time), data FROM {$table} WHERE id=$id LIMIT 1"); if (mysql_num_rows($result) == 0) die('no image'); list($ext, $image_time, $data) = mysql_fetch_row($result); $send_304 = false; if (php_sapi_name() == 'apache') { // if our web server is apache // we get check HTTP // If-Modified-Since header // and do not send image // if there is a cached version $ar = apache_request_headers(); if (isset($ar['If-Modified-Since']) && // If-Modified-Since should exists ($ar['If-Modified-Since'] != '') && // not empty (strtotime($ar['If-Modified-Since']) >= $image_time)) // and grater than $send_304 = true; // image_time } if ($send_304) { // Sending 304 response to browser // "Browser, your cached version of image is OK // we're not sending anything new to you" header('Last-Modified: '.gmdate('D, d M Y H:i:s', $ts).' GMT', true, 304); exit(); // bye-bye } // outputing Last-Modified header header('Last-Modified: '.gmdate('D, d M Y H:i:s', $image_time).' GMT', true, 200); // Set expiration time +1 year // We do not have any photo re-uploading // so, browser may cache this photo for quite a long time header('Expires: '.gmdate('D, d M Y H:i:s', $image_time + 86400*365).' GMT', true, 200); // outputing HTTP headers header('Content-Length: '.strlen($data)); header("Content-type: image/{$ext}"); // outputing image echo $data; exit(); } ?> <html><head> <title>MySQL Blob Image Gallery Example: Tutorial By Mistonline.in</title> </head> <body> <?php if (isset($msg)) // this is special section for // outputing message { ?> <p style="font-weight: bold;"><?php echo $msg?> <br /> <a href="<?php echo $_SERVER['PHP_SELF']?>">reload page</a> <!-- I've added reloading link, because refreshing POST queries is not good idea --> </p> <?php } ?> <h1>Blob image gallery</h1> <h2>Uploaded images:</h2> <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> <!-- This form is used for image deletion --> <?php $result = mysql_query("SELECT id, image_time, title FROM {$table} ORDER BY id DESC"); if (mysql_num_rows($result) == 0) // table is empty echo '<ul><li>No images loaded</li>'; else { echo '<ul>'; while(list($id, $image_time, $title) = mysql_fetch_row($result)) { // outputing list echo "<li><input type='radio' name='del' value='{$id}'/>"; echo "<img width='100' src='".$_SERVER['PHP_SELF']."?show=$id'><br><a href='".$_SERVER['PHP_SELF']."?show=$id'>{$title}</a> – "; echo "<small>{$image_time}</small></li>"; } echo '</ul>'; echo '<label for="password">Password:</label><br />'; echo '<input type="password" name="password" id="password"/><br /><br />'; echo '<input type="submit" value="Delete selected"/>'; } ?> </form> <h2>Upload new image:</h2> <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST" enctype="multipart/form-data"> <label for="title">Title:</label><br /> <input type="text" name="title" id="title" size="64"/><br /><br /> <label for="photo">Photo:</label><br /> <input type="file" name="photo" id="photo"/><br /><br /> <label for="password">Password:</label><br /> <input type="password" name="password" id="password"/><br /><br /> <input type="submit" value="upload"/> </form> </body> </html>
Thats it. You are done. Here you should give the password as 123. You can find that in the code. The page will look like this once you do some uploading.
Thanks!!! 🙂
PHP MYSQL example image gallery (blob-storage),Incoming search terms:
- yhse-001 (4)
- Imagegalleryusingphpandmysqlblobstorageanddisplayingtheimagefrommysqlblob|WebsiteScriptsAndTutorials (4)
- insert and display image gallery with mysql php (3)
- Imagegalleryusingphpandmysqlblobstorageanddisplayingtheimagefrommysqlblob (3)
- php tutotial to add an image from mysql database to slideshow in php (2)
- $_Server code image uploading using blob datatype in php (2)
- displaying image on php page from mysql (2)
- Imagegalleryusingphpandmysqlblobstorageanddisplayingtheimagefrommysqlblob-BestOnlineSolutionForWebsiteScriptsAndTutorials (2)
- display mysql images in a photo gallery (2)
- PHPArchives-BestOnlineSolutionForWebsiteScriptsAndTutorials (2)
- how to update one images b/w two images in php (1)
- IMAGEN MYSQL JSP (1)
- imagesLoaded in jquery php mysql (1)
- Image upload gallery with admin section in php (1)
- insert image demo in php database (1)
- insert image php mysql templete (1)
- insert images in mysql using php (1)
- insert update delete image in php mysql (1)
- insert update delete view search with image code php mysql (1)
- jquery script for displaying gallery images from mysql database (1)
- jquery:display blob from mysql (1)
- li image from mysql (1)
- how to retrieve my picture from the database and display using any image format php (1)
- how to retrieve blob data from mysql database in php (1)
- how to insert update delete display image in php and mysql photogallery (1)
- display blob php (1)
- display image from database in php mysql by click on next & previous button example (1)
- display video blob php (1)
- DOWNLOAD CODE IN PHP TO INSERT UPDATE DELETE WITH IMAGE FILE (1)
- download coding for insert and display image in php (1)
- download Image Upload using PHP/MySQL (1)
- example and demo to locate and display image from mysql database using php (1)
- gallery php blob (1)
- how to create mysql jquery image gallery (1)
- how to display blob image from mysql in flex (1)
- how to display blob image in jqgrid (1)
- how to display image and text from database in php like Facebook (1)
- how to display image website code on php (1)
- how to display picture with text in php (1)
- how to insert and update images in mysql using php (1)
Thx for this great example! It really works (except the Create-Table-Stmt … ther’s a ‘)’ to much). But anyway – yesterday I’ve spent hours to find out, why my similar script with PHP5.3 and MySQL did not work. You had the solution! Thanks a lot! It’s really a great Script.
how to retrive the images in to my webpage from data base?
thanks
Hello Rajesh,
I have added that section in the code now. It is very easy instead of dynamically adding the image ID in the A HREF tag we can use IMG SRC instead. and call the image..
Hi.. I am stuck and I’m a total newbie at php / mysql .. Would be grateful if someone could help me .. how do I add a function to update the title belonging to the individual picture? Thanks in advance! 🙂
Hello Paso,
First of all thank you very much for your comment. Sorry i didn’t exactly understood what you really need but i think you are asking about image title attribute. If that is the case then all you have to do is change the code where we retrieve image from Mysql
To
If otherwise please let me know?
Thanks for your quick reply .. I was not clear enough in my question .. sorry ..
I need help with an update function ..
Instead of deleting a photo and upload it again “just because I need to change the imagetitle or text “I just want to be able to update these title/text-rows !? But I do not know how! 🙁
I guess it could look like this ..
if (isset ($ _POST [‘update’])) // If used selected some photo to update
{// In ‘uploaded images form’;
$ id = intval ($ _POST [‘update’]);
mysql_query (“UPDATE {$ table} SET title = ‘$ title’, text = ‘$ text’ WHERE id = $ id”);
$ msg = ‘ Title and Text updated ‘;
}
I hope you now understand what I mean! 🙂
Sorry for my very bad explanation .. that I would need help with is …
I guess it could start with this? 😉
mysql_query (“UPDATE {$ table} SET title, text WHERE id = $ id”)
I hope you understand what I am looking for and can help me !? 🙂
Not a problem, All you need to do is create a html form and use 2 input fields one for new title and other for old. Hide the old title input field using simple css.
Once all set, Use the below mysql query inside the php code that has the GET or POST functionality.
Now it was a while since I tried to get my problem solved .. Thanks for your help so far .. But I soon give up .. I’ve tried making a new form and do as you said .. but nothing I do seems to work ! I’m doing something wrong! ” .. Can you please show me how to do this? Pleeeease !! 🙂 I would really appreciate it!
I just want to update the title and the text belonging to my pictures!
I’m pretty annoyed at myself right now !! I mean .. how hard can it be? 🙂
where the uploaded photos will be displayed?
Please refer previous comment. I have updated the code accordingly. It will display the images in the “Uploaded Images” section. Please test it from your side.
Thanks dear.
Superb. Thanks a lot.
I want able to change photo and title. i follow above comment but still not success.. can you send me the complete code for able to change photo and title.. Thanks
how to change existing photo?
thanks for this tutorial
which code editor do you use to write code???its very nice.
My thumbnails display a broken image and when I click on the link it tells me it cannot be displayed because it contains errors. Any suggestions would be greatly appreciated! Thank you
Hello Deanna,
Thanks for your comment. Happy to see that you have implemented one of our scripts on your page 🙂
Please let me know in detail the error you are seeing?
Regards
Admin
I like this source code : visit my shopping site that is developed by me.