codeigniter从不同的输入上传图像

时间:2017-07-05 13:08:52

标签: codeigniter upload image-upload

我的代码在单个上传中运行良好,但是我想为我的代码创建一个循环,因为使用了两个不同的输入,我不会反复重复创建一个函数。

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/>`

1 个答案:

答案 0 :(得分:0)

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

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');