How to insert image in Db as blob in ZendFramework

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

+3
source share
3 answers

In your Zend form, create a file element:

$element = new Zend_Form_Element_File('fileElement');

Then you can insert the downloaded image into your database in BLOB format as follows:

$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(); 
+2
source

Read it as a string using file_get_contentsand save it.

However, it is very rarely worth storing the actual image data in a database. It is best to create a unique file name and store it in the database.

+2
source

zend:

// This is to save in saveAction()
$source = $this->view->form->upload->getFileName();
$fp = fopen($source,'r');
$content =   fread($fp,filesize($source));   
// I don't use addslashes, because i already have that in my mapper

fclose($fp); 

$model->image       = base64_encode($content);
// Save here


// This is to read in imagesAction()
$this->_helper->Layout()->disableLayout();
$this->_helper->ViewRenderer->setNeverRender();

header('Content-Type: image/gif');
echo base64_decode($image);     
exit;

, , . , db ( ).

0

Source: https://habr.com/ru/post/1714302/


All Articles