我想使用Codeigniter将两个或多个图片在单独的字段中使用一个表单上传到数据库中 .
但这里只有一个正在上传..任何人都可以请帮助我..
我的控制器
class Products extends CI_Controller {
public function __construct()
{
// Call the CI_Model constructor
parent::__construct();
$this->load->model('Product_model');
}
public function save()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = 5024;
$config['encrypt_name'] =TRUE;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
echo var_dump($error ); die;
}
else
{
$file_data = array('upload_data' => $this->upload->data());
if($this->Product_model->addProducts($file_data))
{
$this->load->view('success_view');
}
else
{
$this->load->view('failure_view');
}
}
这是我的模特
public function addProducts($file_data)
{
$data=array(
'pr_name'=>$_POST['pr_name'],
'pr_code'=>$_POST['pr_code'],
'photo_file'=>$file_data['upload_data']['file_name'],
'photo_file2'=>$file_data['upload_data']['file_name'],
);
return $this->db->insert('products', $data);
}
这是我的观点
<div class="container">
<div class="row">
<div class="col-md-6">
<form class="form-horizontal" method="post" enctype="multipart/form-data" action="<?php echo site_url('Products/save');?>">
<div class="form-group">
<label for="exampleInputEmail1">Product Name</label>
<input type="text" name="pr_name" class="form-control" id="exampleInputEmail1" placeholder="Product Name">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Product Model</label>
<input type="text" name="pr_code" class="form-control" id="exampleInputPassword1" placeholder="Product Model">
</div>
<div class="form-group">
<label for="exampleInputFile">Product Image 1</label>
<input type="file" name="userfile" id="exampleInputFile" >
</div>
<div class="form-group">
<label for="exampleInputFile">Product Image 2</label>
<input type="file" name="userfile2" id="exampleInputFile" >
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
</div>
我的数据库
CREATE TABLE `products` (
`pr_id` int(5) NOT NULL,
`pr_name` varchar(200) NOT NULL,
`pr_code` varchar(200) NOT NULL,
`photo_file` varchar(255) NOT NULL,
`photo_file2` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
请帮我上传到单独的数据库字段作为两个单独的文件
1 回答
使用此功能,享受;)