【问题标题】:codeigniter upload images from different inputscodeigniter 从不同的输入上传图像
【发布时间】:2017-07-05 13:08:51
【问题描述】:

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

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

【问题讨论】:

    标签: codeigniter upload image-upload


    【解决方案1】:

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

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

    【讨论】:

    • 如果我想将 $fileName_1 存储到数组中怎么办。像那些? '$data = array('UserFile' => $this->uploadImage(), 'UserFile1' => $this->uploadImage_2());'
    • @HanthonyTagam 你可以这样使用: $data = array( 'UserFile1' => $this->uploadImage('userfile1'), 'UserFile2' => $this->uploadImage('用户文件2') );
    • 之前已经这样做了,它只显示了两个相同的结果
    • @HanthonyTagam 在上述数组之后使用此代码并向我展示你得到的输出:var_dump($data);
    • array(3) { ["userId"]=> string(2) "31" ["UserFile"]=> string(28) "Avengers-Giant-Man-ico44.png" [ "UserFile1"]=> string(28) "Avengers-Giant-Man-ico45.png" }
    猜你喜欢
    • 2015-07-01
    • 2018-02-28
    • 2012-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-30
    • 2015-03-17
    • 1970-01-01
    相关资源
    最近更新 更多