【问题标题】:How to create an array in file upload如何在文件上传中创建数组
【发布时间】:2016-12-23 16:27:40
【问题描述】:

我想创建一个上传图片的函数。我这里的代码一次只上传一张文件图片,我想创建一个可以上传多张图片的数组(例如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>

【问题讨论】:

  • 在您的模型中,您需要创建 for 循环,然后对其进行迭代
  • 我编辑了我的帖子,我把我的模型放在那里

标签: php codeigniter model-view-controller


【解决方案1】:

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

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
}

【讨论】:

  • 喜欢这个? $fileCount = count($image["name"]); for ($i = 0; $i $this->input->post('name'), 'org_description' => $this->input->post('description'), 'org_image' => $this ->input->post('userfile'), 'org_image' => $this->upload->data('file_name') ); }
  • @Holow 我在上面的答案中首先提到了您插入其他详细信息。要存储多个图像,您需要有单独的表。并存储图像名称以及最后插入的组织详细信息 ID。
  • 所以我将有 2 个表(一个是 organization_table,另一个是 tblgallery)然后在我的 tblgallery 中的列是 gallery_id 和 gallery_name?
  • @Holow 画廊表中的另一列organization_id
  • 所以我将组织 ID(在 tblgallery 中)作为外键,然后在我的组织表中作为主键?
猜你喜欢
  • 2023-01-05
  • 2016-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-19
  • 1970-01-01
  • 2011-06-23
相关资源
最近更新 更多