473,408 Members | 1,747 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

How to store the images in mysql table and retrieval using php?

Hi

i need help that how to store the images in the mysql table and retrieval of stored image using php code...

sample code that helps me a lot..

regards
~Deepu~
Sep 20 '10 #1
2 2336
Dormilich
8,658 Expert Mod 8TB
this should get you started
Sep 20 '10 #2
Hi deepu,

For Image Entry And Retrieval You Need Two PHP Scripts...
1.addimage.php-To Add Image Details In Database And the image in Webserver.
2.showimage.php-To view Image Retrieving Data From Database..

1.addimage.php

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head></head>
  3. <body>
  4.  
  5. <?php
  6. //define uploadpath Constant
  7. //In This Case We Upload Image In A Folder Named "images" in the webroot
  8. define('UPLOADPATH','images/');
  9. define('MAXSIZE','50786');
  10. if(isset($_POST['upload'])) {
  11. $image=$_FILES['image']['name'];
  12. $imagemime=$_FILES['image']['type'];
  13. $imagesize=$_FILES['image']['size'];
  14. //rename the image name to avoid duplication
  15. $modimage=md5('$image');
  16. $finalname=$modimage'.'$imagemime;
  17.  
  18. if(!empty($finalname) && !empty($imagemime) $$ !empty($imagesize)) {
  19. //allow only jpg and gif extension and file greater than 0 bytes
  20. if(($imagemime == 'image/jpg' || $imagemime == 'image/gif') && $imagesize > 0 && $imagesize <= MAXSIZE && $_FILES['image']['error'] == 0) {
  21. $throw=UPLOADPATH.$finalname;
  22. if(move_uploaded_file($_FILES['image']['tmp_name'],$throw)) {
  23. $dbc=mysqli_connect('localhost','username','password','dbname');
  24. $query="INSERT INTO image (image_id,image_name,image_mime,image_size) VALUES (0,'$finalname','$imagemime','$imagesize')";
  25. mysqli_query($dbc,$query) ordie('Error Querying MySql');
  26. echo '<p> Your Image '.$image.'Has Been SuccessFully Uploaded.</p><br />';
  27. echo '<img src="'.UPLOADPATH.$finalname.'"/>';
  28. $image='';
  29. mysqli_close($dbc);
  30. else {
  31. echo '<p>Problem Encountered !!</p>';
  32. unlink($_FILES['image'][tmp_name']);
  33. }
  34. }
  35.  else {
  36.    echo '<p>Your Image Should Be Either Of JPEG OR GIF Format And Less Than 50 KB</p>';
  37.  }
  38. }
  39. ?>
  40. <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
  41. <input type="hidden"  name="MAXSIZE" value="<?php echo MAXSIZE; ?>" />
  42. <br />
  43. Choose Image :<br />
  44.  
  45. <input type="file" name="image" /> &nbsp;
  46. <input type="submit" value="Upload" name="upload" />
  47. </form>
  48. </body>
  49. </html>
Expand|Select|Wrap|Line Numbers
  1. ### CREATE TABLE image In Database ###
  2. CREATE TABLE 'image' (
  3. 'image_id' INT AUTO_INCREMENT,
  4. 'image_name' VARCHAR(48),
  5. 'image_mime' VARCHAR(8),
  6. 'image_size' VARCHAR(8),
  7. PRIMARY KEY('image_id')
  8. );
Sep 24 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: cover | last post by:
The question is, we have two options to store images, either in a Database (MySQL, Postgres, ...) like blob data, or in the hard disk the file and the path in database. Which option is better?...
9
by: Dejan | last post by:
Hy, Sorry for my terreble english I have this simple code for deleting rows in mysql table... Everything works fine with it. So, what do i wanna do...: my sql table looks something like...
4
by: whitemoss | last post by:
Hi, I've made some changes to my coding..but unfortunately, there were errors when compiling it..dunno how to solve it..hope anyone can help me...the errors: client.c: In function senddata:...
0
by: Big George | last post by:
I'm developing with ASP.NET, VS.NET 2003. Using Crystal Report for VS.NET DataBase: Oracle 10g I store a BLOB field in a table. This field store images, like jpg files. The Stored Procedure...
10
by: eholz1 | last post by:
Hello Members, I am setting up a photo website. I have decided to use PHP and MySQL. I can load jpeg files into the table (medium blob, or even longtext) and get the image(s) to display without...
1
osward
by: osward | last post by:
Hi all, I got code over the net for paging mysql table, it provides Prev pages Next link at the bottom of the table. However, I have a pretty large table to display (average over 400+ rows). Even...
3
by: printline | last post by:
Hello All I need a little help with a phph script to display some specific data from a mysql table. I have a mysql table with 4 columns and 10 rows. I want to display fx. data from row 4, 6, 8...
13
by: ramprakashjava | last post by:
hi, i hav "java.lang.NullPointerException" error while Deleting table records using checkbox in jsp here i enclosed files help quickly plzzz.. ...
3
by: Chuck Beck | last post by:
I am struggling with this issue right now. I have read in a number of places that you should not store the image itself in the database, but rather only the filepath/filename. I can't seem to...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.