« Functions PDO PHP 5.1 | Home | Tak Perlu Dibesar-besarkan dan Tak Perlu Ditanggapi »
Saving Images into MySQL Database
By Abu Aisyah | March 31, 2008
Actually this question has been frequently asked with me to forums, even in infolinux, or the other PHP & MySQL forums. And they are nobody can answer it. Eventough there are making a not fine tutorial. But Alhamdulillah, at last I found the way and the right way to save an image to database.
Saving image files or mp3 or video, we usually known a data type name BLOB (Binary Large Objects). BLOB found in many database where it can handle big of transaction of data, like Oracle, MySQL, MS SQL Server,etc.
OK, firstly we need a table.
CREATE TABLE myimage(
id int(11) auto_increment primary key,
name varchar(45),
gambar blob
);
Making a PHP Script. Example we name it save.php
I use 2 way to save images. First using PDO Function, and the second is ordinary saving with OOP. I using EzSQL class for OOP database, you can find it on internet. Visit PHPclasses site to download it.
1. Using PDO
<html>
<head><title>Try Image</title></head>
<body>
<?php
$db= new PDO('mysql:host=localhost;dbname=doni', 'root', '');
if ($_POST['submit']){
$fp = fopen($_FILES['image']['tmp_name'],’rb’);
$s = “insert into myimage (name, image) values (?,?)”;
$stmt= $db->prepare($s);
$stmt->bindParam(1,$_FILES['image']['name']);
$stmt->bindParam(2,$fp,PDO::PARAM_LOB);
$db->beginTransaction();
$stmt->execute();
$db->commit();
}
?>
<form method=post enctype=”multipart/form-data”>
<input type=’file’ name=’image’ />
<input type=’hidden’ name=’MAX_FILE_SIZE’ value=’3000000′ />
<input type=’submit’ name=’submit’ value=’Save’ />
</form>
<?php
$db = null;
?>
</body>
</html>
2. Using ordinary saving
<html>
<head><title>Try Image</title></head>
<body>
<?php
require("config.php");
require("class.php");
if ($_POST['submit']){
$fp = fopen($_FILES['image']['tmp_name'],’rb’);
$t = file_get_contents($_FILES['image']['tmp_name']);
$s = “insert into myimage (name, image) values (’”.$_FILES['image']['name].”‘, ‘”.addslashes($t).”‘)”;
$db->query($s);
}
?>
<form method=post enctype=”multipart/form-data”>
<input type=’file’ name=’image’ />
<input type=’hidden’ name=’MAX_FILE_SIZE’ value=’3000000′ />
<input type=’submit’ name=’submit’ value=’Save’ />
</form>
</body>
</html>
Then the way to display it, its very easy too, we just use header function. We make script display.php
I will give you 2 way too, using PDO and ordinary way.
1. Using PDO
<?php
$db= new PDO('mysql:host=localhost;dbname=doni', 'root', '');
$stmt = $db->prepare("select name, image from myimage where id = ?");
$stmt->execute(array($_GET['id']));
$stmt->bindColumn(1,$name);
$stmt->bindColumn(2,$lob,PDO::PARAM_LOB);
$stmt->fetch(PDO::FETCH_BOUND);
header(”Content-Type: image/jpg”);
echo $lob;
?>
2. Using ordinary way
<?php
require("config.php");
require("class.php");
$s = “select * from myimage where id = 1″;
$gmbr = $db->get_row($s);
header(”Content-Type: image/jpg”);
echo $gmbr->image;
?>
Yap, thats it. Very easy right? Maybe, I need more to learn, because I missed this thing. ![]()
Topics: Linux, Ngoprek, Open Source, PHP, Programming, mysql |


June 10th, 2008 at 10:38 am
Sepertinya script di atas kurang tepat soalnya waktu tak coba hasilnya error. coba aja script berikut yang saya coba berhasil.
tabelnya
CREATE TABLE `images` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`image` BLOB NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM;
$image = chunk_split(base64_encode(file_get_contents(”image.jpg”)));
$query = “INSERT INTO images (image) VALUES(’$image’)”;
mysql_query($query) or die(mysql_error());
cara nampilinnya
header(’Content-type: image/jpeg’);
$query = “SELECT image from images where id=1″;
$rs = mysql_fetch_array(mysql_query($query));
echo base64_decode($rs["image"]);
October 28th, 2008 at 3:34 pm
# at last I found the way and the right way to save an image to database
Actually there’s no correct way to save an image to database. Its a database performance killer, and an abuse to the database itselves.
You don’t save the image content into your database, store the path, not the content.
November 6th, 2008 at 12:20 pm
And you do not accidentally from Moscow?
November 8th, 2008 at 4:05 pm
Salam.. I agreed with ferdhie, it is better to save the path in the database rather keeping the file itslef.. Other than the performance issue, database size will also be a problem as it accumulate more file inside..
Store the file in separate folder and keep the path in the database..