views:

760

answers:

2

I want to insert a image in blob format to db. how to write a program in zend framework. a simple form just choose one image and insert into db.

+2  A: 

Read it in as a string using file_get_contents and store that.

That said, it is seldom a good idea to store the actual image data in the database. It is better practice to generate a unique filename and store that in the DB instead.

karim79
Agreed, BLOB in DB will often be slow and contribute to DB bloat.
Ben Reisner
is there any additional function or method while insert a blob into db via Zend framework ? or simple Model class structure? i am unable to insert blob field, rest other fields are inserted but only problm is with the blog
coderex
A: 

In your Zend form create a file element:

$element = new Zend_Form_Element_File('fileElement');

Then you can insert the uploaded image to your DB in BLOB format like this:

$conn = new PDO("mysql:host='host';dbname='database'", 'userName', 'password'); 

$imagePath = $zendForm->fileElement->getFileName(); 
$image = file_get_contents('$imagePath'); 

$sql = "INSERT INTO images (data) values(?)"; 

$q = $conn->prepare($sql); 
$q->bindParam(1, $image, PDO::PARAM_LOB); 
$q->execute();
Michael Alves
ooo am sorry, i am using Zend model class and $tablename = new Zend_model_Photo();$tablename->save();this way i am save the data... so i cant mention the "$sql = "INSERT INTO images (data) values(?)"; " over there....
coderex