codeigniter 从不同的输入上传图像

codeigniter upload images from different inputs

我的代码在单次上传中运行良好,但我想为我的代码创建一个循环,这样我就不会因为使用两个不同的输入而一遍又一遍地重复创建一个函数。

public function uploadImage_1()
{       
    $config['upload_path'] = './uploads';
    $config['allowed_types'] = 'gif|jpg|png|jpeg';
    $config['max_size'] = '2608';
    $config['max_width'] = '2608';
    $config['max_height'] = '2608';
    $this->load->library('upload', $config);
    if ( !$this->upload->do_upload('userfile1')){
        $error = array('error' => $this->upload->display_errors());
    }else{
        $fileName = $this->upload->data();
        $post_image = $fileName['file_name'];
        return $post_image;
    }
}

public function uploadImage_2()
{}

` 查看

<input type="file" name="userfile1" size="20" required/>
<input type="file" name="userfile2" size="20" required/>`

您可以将输入字段名称作为参数传递给您的函数。
像这样:

public function uploadImage($input_field_name)
{       
    $config['upload_path'] = './uploads';
    $config['allowed_types'] = 'gif|jpg|png|jpeg';
    $config['max_size'] = '2608';
    $config['max_width'] = '2608';
    $config['max_height'] = '2608';
    $this->load->library('upload', $config);

    if ( !$this->upload->do_upload($input_field_name)){
        $error = array('error' => $this->upload->display_errors());
    }else{
        $fileName = $this->upload->data();
        $post_image = $fileName['file_name'];
        return $post_image;
    }
}


现在像这样使用上面的函数:

// These variables stores the name of your files
$fileName_1 = $this->uploadImage('userfile1');
$fileName_2 = $this->uploadImage('userfile2');