my issue is that i insert multiple image in one row with comma separated in codeginter

时间:2018-04-25 05:20:43

标签: php jquery ajax codeigniter

I am so tried to insert to multiple images its work perfectly but what i want to multiple images insert in to database with one row means comma separated like that for example id =4 and img = img1,img2,img3,img4 i want insert in one row in codeginter but don't know how to use this function $data= implode(",",$userfile); THANKS advances brother

Here is my controller function

function blog_img_new()
{
    $imgtest = $this->blog->image_get_test();
    $this->template->load_sub('imgtest', $imgtest);
    $this->template->load('admin/test-imag');
}

function blog_img()
{
    $number_of_file = sizeof($_FILES['userfile']['tmp_name']);
    $file = $_FILES['userfile'];

    // Faking upload calls to $_FILE
    for ($i = 0; $i < $number_of_file; $i++) :

        $_FILES['userfile']['name']     = $file ['name'][$i];
        $_FILES['userfile']['type']     = $file ['type'][$i];
        $_FILES['userfile']['tmp_name'] = $file ['tmp_name'][$i];
        $_FILES['userfile']['error']    = $file ['error'][$i];
        $_FILES['userfile']['size']     = $file ['size'][$i];

        $config['upload_path'] = './photo/uploads'; //The path where the image will be save
        $config['allowed_types'] = 'gif|jpg|png';
        $this->load->library('upload', $config);

        $this->upload->initialize($config);
        $this->upload->do_upload('userfile');

        $data = $this->upload->data();

        $file_name[] = $this->upload->data();

        $data = array(
            'userfile'   => $this->upload->data('file_name'),
        );

        $data= implode(",",$userfile);
        $this->blog->blog_img($data);

            //redirect('/admin/blog/img/insert');
    endfor;

}

this is my model function

function blog_img($data)
{
    $userfile = addslashes($data['userfile']);
    return $this->db->query("INSERT INTO filename_img (userfile) VALUES ('$userfile')");
}

view page

<input type="file"  name="userfile[]" id="userfile" multiple >

1 个答案:

答案 0 :(得分:0)

您必须将插入功能移出循环。在循环中,您必须将每个文件名分配给您可以内爆的数组。

    function blog_img()
    {
        $number_of_file = sizeof($_FILES['userfile']['tmp_name']);
        $file = $_FILES['userfile'];

        $files = array();

        // Faking upload calls to $_FILE
        for ($i = 0; $i < $number_of_file; $i++) :

            $_FILES['userfile']['name']     = $file ['name'][$i];
            $_FILES['userfile']['type']     = $file ['type'][$i];
            $_FILES['userfile']['tmp_name'] = $file ['tmp_name'][$i];
            $_FILES['userfile']['error']    = $file ['error'][$i];
            $_FILES['userfile']['size']     = $file ['size'][$i];

            $config['upload_path'] = './photo/uploads'; //The path where the image will be save
            $config['allowed_types'] = 'gif|jpg|png';
            $this->load->library('upload', $config);

            $this->upload->initialize($config);
            $this->upload->do_upload('userfile');

            //$data = $this->upload->data();

            $files[] = $this->upload->data('file_name');

            //$data= implode(",",$userfile);
            //$this->blog->blog_img($data);

                //redirect('/admin/blog/img/insert');
        endfor;

        $data= implode(",",$files);
        $this->blog->blog_img($data);
}

使用QB的模型:

function blog_img($files)
{
    $this->db->set('userfile', $files);
    return $this->db->insert('filename_img');
}