How to Image Store and Update on database and view stored image from database using Cakephp ?
1> create a database table as follows.
— Table structure for table `users`
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(100) DEFAULT NULL,
`email` varchar(150) DEFAULT NULL,
`firstname` varchar(60) DEFAULT NULL,
`lastname` varchar(60) DEFAULT NULL,
`img_name` varchar(25) NOT NULL,
`image` blob NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;
2> create a model on your app/models folder
//model for the abave table
class User extends AppModel {
var $name = ‘User’;
var $hasMany = array(‘Post’);
}
3> create a controller as following on app/controllers folder.
//my TestUsers controller
class TestUsersController extends Controller {
var $name = ‘TestUsers’;
var $uses = array(‘User’);
var $helpers = array(‘Html’, ‘Form’);
var $components = null;
/*
* method: This method is for upload and store an image to database table.
* @arguments: null
* @return: null
*/
function add(){
if($this->data &&is_uploaded_file($this->data[‘File’][‘image’][‘tmp_name’]) ){
$this->data[‘User’][‘img_name’] = $this->data[‘File’][‘image’][‘name’];
$fileData = fread(
fopen($this->data[‘File’][‘image’][‘tmp_name’], “r”),
$this->data[‘File’][‘image’][‘size’]
);
$this->data[‘User’][‘image’] = $fileData;
if($this->User->save($this->data[‘User’])){
echo “File uploaded successfully”;
}else{
echo “File does not uploaded successfully”;
}
}
}
/*
* method: This method is for edit the uploaded and stored image to database table and show the upload image from database.
* @arguments: null
* @return: null
*/
function edit($id=null){
if($this->data &&is_uploaded_file($this->data[‘File’][‘image’][‘tmp_name’]) ){
$this->data[‘User’][‘img_name’] = $this->data[‘File’][‘image’][‘name’];
$fileData = fread(
fopen($this->data[‘File’][‘image’][‘tmp_name’], “r”),
$this->data[‘File’][‘image’][‘size’]
);
$this->data[‘User’][‘image’] = $fileData;
if($this->User->save($this->data[‘User’])){
echo “File uploaded successfully”;
}else{
echo “File does not uploaded successfully”;
}
}else{
$this->data = $this->User->read(null,$id);
}
}
/*
* method: this method is for retrive the image from database.
* this is action will be on your image sourse.
* (here it is : project_nam/TestUsers/image)
* @arguments: null
* @arguments: null
*/
function image($id){
$this->data = $this->User->read(null,$id);
echo $this->data[‘User’][‘image’];
die;
}
}
4> create view add.ctp on app/views/test_users folder.
<?php e($form->create(‘User’,array(‘url’=>array(‘controller’=>’test_users’, ‘action’=>’add’), ‘type’ => ‘file’, ‘name’=>’test_user_form’))); ?>
<?php e($form->input(‘name’)); ?>
<?php e($form->input(’email’)); ?>
<?php e($form->input(‘firstname’)); ?>
<?php e($form->input(‘lastname’)); ?>
<?php e($form->file(‘File.image’)); ?>
<?php e($form->submit(‘submit’, array(‘style’=>’float:left’, ‘width’=>’100px’))); ?>
<?php e($form->end()); ?>
4> create view edit.ctp on app/views/test_users folder.
<?php e($form->create(‘User’,array(‘url’=>array(‘controller’=>’test_users’, ‘action’=>’add’), ‘type’ => ‘file’, ‘name’=>’test_user_form’))); ?>
<?php if(isset ($this->data[‘User’][‘image’])):?>
<img alt=”img” src=”http://localhost/mycake/test_users/image/<?php echo $this->data[‘User’][‘id’]; ?>” />
<?php endif; ?>
<?php e($form->input(‘name’)); ?>
<?php e($form->input(’email’)); ?>
<?php e($form->input(‘firstname’)); ?>
<?php e($form->input(‘lastname’)); ?>
<?php e($form->file(‘File.image’)); ?>
<?php e($form->submit(‘submit’, array(‘style’=>’float:left’, ‘width’=>’100px’))); ?>
<?php e($form->hidden(‘id’)); ?>
<?php e($form->end()); ?>
now for image insert use:
for edit and view database image use.