Multiple File Upload in cakephp by one user and save the images name in one column in database :
Hello readers, This is a small blog on how to upload multiple file in cakephp. This is very simple and useful code, hope this code will be helpful for our users.
Step 1: Create a table named users in your database
CREATE TABLE `images` (
`id` int(11) NOT NULL PRIMARY KEY,
`name` VARCHAR(45) ,
`email` VARCHAR(255)
);
Step 2: Next create a form having input type file and multiple, set enctype'=>'multipart/form-data. code will be like:
<?php echo $this->Form->create('User', array('enctype' => 'multipart/form-data'));?>
echo $this->Form->input('image.', array('type'=>'file', 'multiple'));
Step 3: Now make a function in your controller (ex-UsersController) and add the following code
$n = count($this->data['User']['image']);
if(!empty($this->request->data['User']['image']))
{
for($i= 0;$i < $n; $i++){
$file=$this->data['User']['image'];
$ary_ext=array('jpg','jpeg','gif','png'); //array of allowed extensions
$ext = substr(strtolower(strrchr($file[$i]['name'], '.')), 1); //get the extension
if(in_array($ext, $ary_ext))
{
move_uploaded_file($file[$i]['tmp_name'], WWW_ROOT . 'upload/' . time().$file[$i]['name']);
$name = time().$file[$i]['name'];
$name_arr[$i] = $name;
}
}
$name_str=implode(',', $name_arr);
}
$this->request->data['User']['image'] = $name_str;
0 Comment(s)