<form method="post" action="<?php echo base_url(); ?>shop/con_add_submit_form">
Product Name: <input class="form-control" type="text" name="product" /><br />
Add Image: <input class="form-control" type="file" name="userfile" /><br />
Description: <input class="form-control" type="text" name="description" /><br />
Quantity:<input class="form-control" type="text" name="quantity" /><br />
Price: <input class="form-control" type="text" name="price" /><br />
<center> <input class="btn btn-primary" type="submit" value="ADD" /> <a class="btn btn-danger" href="<?php echo base_url(); ?>shop">CANCEL</a> </center>
</form>
控制器
public function con_add_submit_form()
{
$post = $this->input->post(); // $_POST;
$userfile = $this->input->post('userfile'); // $_POST;
$config['upload_path'] = './assets/uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100000;
$config['max_width'] = 10000;
$config['max_height'] = 100000;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload($userfile))
{
$error = array('error' => $this->upload->display_errors());
}
else
{
$data = array('upload_data' => $this->upload->data());
}
$this->upload->do_upload('userfile');
$this->data_fetcher->AddUser($post); //calls add user function from data_fetcher model
redirect(base_url().'shop');
}
答案 0 :(得分:0)
<?php echo form_open_multipart('shop/con_add_submit_form');?>
Product Name: <input class="form-control" type="text" name="product" /><br />
Add Image: <input class="form-control" type="file" name="userfile" /><br />
Description: <input class="form-control" type="text" name="description" /><br />
Quantity:<input class="form-control" type="text" name="quantity" /><br />
Price: <input class="form-control" type="text" name="price" /><br />
<center>
<button type="submit" class="btn btn-danger">Add</button>
</center>
<?php echo form_close();?>
文件上传数据https://www.codeigniter.com/user_guide/libraries/file_uploading.html#CI_Upload::data
控制器
文件名:Shop.php
<?php
class Shop extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper('form');
$this->load->library('form_validation');
}
public function con_add_submit_form() {
$data['title'] = 'Welcome to Codeigniter';
$this->form_validation->set_rules('product', 'product', 'trim|required');
if ($this->form_validation->run() == false) {
$this->load->view('your_view', $data);
} else {
if (isset($_FILES['userfile']['name'])) {
$config['upload_path'] = './assets/uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100000;
$config['max_width'] = 10000;
$config['max_height'] = 100000;
$this->load->library('upload', $config);
if ($this->upload->do_upload('userfile')) {
$upload_data = $this->upload->data();
$this->data_fetcher->add_product($this->input->post(), $upload_data);
}
} else {
$this->data_fetcher->add_product($this->input->post());
redirect(base_url('some_controller'));
}
}
public function add_product($data = array(), $upload_data = array()) {
$insert_array = array(
'product' => $data['product'],
'userfile' => ($upload_data) ? $upload_data['file_name'] : '',
'description' => $data['description'],
'quantity' => $data['quantity'],
'price' => $data['price'],
);
$this->db->insert('yourtablename', $insert_array);
}
}