How to save image using PHP MySQL
I have very simple way to store image in mysql databases. I will describe how to save images in MySQL and display it using PHP.
The first step, we need to make a MySQL databases to store our images data. Make a database called “images” on your MySQL database, then create a table called “imagesdata”.
CREATE TABLE `imagesdata` (
`id` int(6) NOT NULL AUTO_INCREMENT,
`image` longtext NOT NULL,
`dates` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
The id field represent id of image, image field represent image, and the dates is field that used to store information when the image uploaded.
There’s 5 file of PHP,
-index.php
-insert.php
-image.php
-viewer.php
-config.php
The index.php is user interface that used for upload an image to mysql database, insert.php is a file that store insertion to database function,the image.php has function returning the image content from database with id parameter, viewer.php is user interface that used for viewing image that stored in our mysql databases, the last but not least the config.php, we store the username, password, and name of database here.
We start with index.php, here’s the code
index.php
<html>
<body>
<form enctype="multipart/form-data" method="POST" action="/insert.php" style="text-align:center">
<label>Image </label><input name="imagefile" type="file"><br/>
<input type="submit" value="Upload">
</form>
</body>
</html>
The index.php have a form that contain filebrowser and submit button. When the submit button pressed, this will do POST method to insert.php
insert.php
<?php
include('config.php');
$image = basename( $_FILES['imagefile']['name']);
$sizeimage=$_FILES['imagefile']['size'];
if(move_uploaded_file($_FILES['imagefile']['tmp_name'], $image))
{
$handle = fopen($image,'r');
$file_content = fread($handle,$_FILES['imagefile']['size']);
fclose($handle);
$encodedimg = chunk_split(base64_encode($file_content));
$sql = "INSERT INTO imagesdata SET image='$encodedimg'";
mysql_query($sql);
unlink($image);
$success=1;
}
else
{
$reason.= "<p>There was an error uploading the file, please try again!</p>";
}
?>
<html>
<body>
<? if($success==1) {?>
<p>Upload success.</p>
<a href="javascript:history. go(-1)">Back</a>
<? }
else {echo $reason;}?>
</body>
</html>
——————————————
image.php
<?php
include('config.php');
$id= $_REQUEST["id"];
$q = 'SELECT (`image`) FROM `imagesdata` where id='.$id;
$result = mysql_query($q);
while ($row = mysql_fetch_array($result) ) {
$img = $row["image"];
}
echo base64_decode($img);
?>
——————————————
viewer.php
<?php
include('config.php');
$q = 'SELECT id FROM `imagesdata`' ;
$result = mysql_query($q) or die(mysql_error());
$content='';
while($row = mysql_fetch_array( $result )) {
$content.= '<img src='.DOMAIN.'/image.php?id='.$row['id'].'>';
}
?>
<html>
<body>
<?echo $content;?>
</body>
</html>
——————————————-
config.php
<?
define('MYSQL_USER', 'root');
define('MYSQL_PASS', '');
define('MYSQL_DB', 'images');
define('MYSQL_HOST', 'localhost');
define('DOMAIN','http://localhost/img/');
$con = mysql_connect(MYSQL_HOST,MYSQL_USER,MYSQL_PASS);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db(MYSQL_DB, $con);
?>
———————————————
You can retrieve your images from database using the id parameter, for example if you want to retrieve image with id=1, you can use <img src=http://yourdomain/image.php?id=1/>
You can download the source here





Comments (5)
joyce
July 29th, 2009 at 7:25 am
you said:
You can retrieve your images from database using the id parameter, for example if you want to retrieve image with id=1, you can use
but how can i call “1″ without literally typing “1″
how can i print that using parameters..
can u tell me?
thanks
leoslab
July 29th, 2009 at 11:15 am
@joyce,
You can change this query $q = ‘SELECT (`image`) FROM `imagesdata` where id=’.$id; in image.php file.
Change the $id with your parameters, or if you want random image, you can randomize the id first.
Maxx82
October 23rd, 2009 at 1:26 am
Very nice and stunning picture. ,
basing
November 29th, 2009 at 6:21 am
nice coding, first record is correct, but in second input with different location picture i have error like this….
Warning: fopen(benderaindonesia.gif) [function.fopen]: failed to open stream: No such file or directory in D:\app259\www\ktpnasional\insert.php on line 10
Warning: fread(): supplied argument is not a valid stream resource in D:\app259\www\ktpnasional\insert.php on line 11
Warning: fclose(): supplied argument is not a valid stream resource in D:\app259\www\ktpnasional\insert.php on line 12
Warning: unlink(benderaindonesia.gif) [function.unlink]: No such file or directory in D:\app259\www\ktpnasional\insert.php on line 22
Upload success.
Back
location of the picture not found…
how to handle that…?
tq.
adiwiguna
April 30th, 2010 at 3:35 am
thanks for the tutorial, how to fit image size?
Leave a reply