我想創建一個函數來上傳圖片。我這裏的代碼是在同一時間只有1個文件圖片上傳,我想創建一個數組,可以上傳多張圖片(例臉譜,製作專輯時,你可以上傳多張圖片)如何在文件上傳中創建數組
我控制器
public function addOrganization()
{
$this->load->view('admin/header');
$this->load->view('admin/sidebar');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2048000';
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="alert alert-danger" role="alert">', '</div>');
$this->form_validation->set_rules('name', 'Name', 'trim|required|is_unique[tblitem.name]');
// $this->form_validation->set_rules('userfile', 'Userfile', 'trim|required|');
$this->form_validation->set_rules('description', 'Description', 'trim|required|min_length[5]');
if ($this->form_validation->run() == FALSE || !$this->upload->do_upload('userfile'))
{ //if validation is false go to itemList
$this->load->view('plmar/admin/addOrganization');
}
else
{
$organization = array(
'org_name' => $this->input->post('name'),
'org_description' => $this->input->post('description'),
'org_image' => $this->input->post('userfile'),
'upload_data' => $this->upload->data()
);
$this->adminModel->addOrganization($organization); //it will add the data of $item in the add function in the model
redirect('Administrator/addOrganization','refresh');
}
$this->load->view('admin/footer');
}
我的模型
public function addOrganization($organization)
{
$organization = array(
'org_name' => $this->input->post('name'),
'org_description' => $this->input->post('description'),
'org_image' => $this->input->post('userfile'),
'org_image' => $this->upload->data('file_name')
);
$this->db->insert('tblorganization', $organization);
}
我查看
<div class="container-fluid">
<br><br><br><br><br><br><br><br><br>
<div class="row">
<div class="col-sm-3"></div>
<div class="col-sm-6">
<form method="post" enctype="multipart/form-data" action="<?php echo base_url().'Administrator/addOrganization' ?>">
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Organization" value="<?php echo set_value('name'); ?>">
<?php echo form_error('name');?>
</div>
<div class="form-group">
<label>Image</label>
<input type="file" required class="form-control" id="userfile" name="userfile">
</div>
<br>
<div class="form-group">
<?php echo form_error('userfile');?>
<label>Description</label>
<textarea class="form-control" rows="3" name="description"><?php echo set_value('description'); ?></textarea>
<?php echo form_error('description');?>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
你需要創建一個循環,然後重複穿透式它 – SoftwareDev
我編輯我的職務,我把我的模型有 – Holow