如何在文件上传中创建数组

时间:2016-08-17 06:23:24

标签: php codeigniter model-view-controller

我想创建一个上传图片的功能。我这里的代码一次只上传1个文件图片,我想创建一个可以上传多个图像的数组(例如facebook,创建相册时可以上传多个图像)

我的控制器

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>

1 个答案:

答案 0 :(得分:0)

在您的模型中作为$image的另一个参数以及文件的$organization。 成功插入$ organization后,请last insert id。 然后计算第二个参数,即$fileCount = count($image["name"]); 然后循环为

for ($i = 0; $i < $fileCount; $i++){
   $imagename = $image["name"][$i]; // will get image name in loop
  // rest of the move_uploaded_file ,insert into db code goes here
}