如何使用数据库在codeigniter中上传多个图像

时间:2018-05-02 13:59:12

标签: php codeigniter

首先,我尝试过简单地上传一张图片,然后我尝试使用相同的方法上传多张图片,但图片正确上传到服务器,但只有一张图片名称上传到数据库。然后我在google上搜索并尝试使用for循环,但它也显示未定义的索引错误。

public function publish_post(){
        // publish post
            if ( $this->input->post('add_post') != '' ) {
                $this->form_validation->set_rules('p_name', 'Product Name','trim|required');
            $this->form_validation->set_rules('p_price', 'Product Price','trim|required');
            $this->form_validation->set_rules('p_currency_type', 'Currency Type');
            $this->form_validation->set_rules('p_category','Category','required');
            $this->form_validation->set_rules('p_menu','Menu','required');
            $this->form_validation->set_rules('p_country','Country','required');
            $this->form_validation->set_rules('p_state','State','required');
            $this->form_validation->set_rules('p_city','City','required');
            $this->form_validation->set_rules('p_zipcode','Zip Code','required');
            $this->form_validation->set_rules('p_description','Product Description','trim|required');

            // $config = ['upload_path' => './assets/postimg','allowed_types' => 'jpg|png|jpeg'];
                //$this->load->library('upload',$config);




            if ( $this->form_validation->run() /*&& $this->upload->do_upload('p_img1') && $this->upload->do_upload('p_img2') && $this->upload->do_upload('p_img3') && $this->upload->do_upload('p_img4') */) {
                // if validation OK then run this code


                $config = ['upload_path' => './assets/postimg','allowed_types' => 'jpg|png|jpeg'];
                $this->load->library('upload');
                $dataInfo = array();
                $files = $_FILES;
                $cpt = count($_FILES['userfile']['name']);
                for($i=0; $i<$cpt; $i++)
                {           
                    $_FILES['userfile']['name']= $files['userfile']['name'][$i];
                    $_FILES['userfile']['type']= $files['userfile']['type'][$i];
                    $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
                    $_FILES['userfile']['error']= $files['userfile']['error'][$i];
                    $_FILES['userfile']['size']= $files['userfile']['size'][$i];    

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

                    $dataInfo['userfile'] = $_FILES['userfile']['name'];

                    $files = array(

                        'p_img1' => $dataInfo[0],
                        'p_img2' => $dataInfo[1],
                        'p_img3' => $dataInfo[2],
                        'p_img4' => $dataInfo[3],
                        'owner_ins'=>$dataInfo[4]
                    );

                    $this->usermodel->add_post($files);

                }



                $p_name = $this->input->post('p_name');
                $p_price = $this->input->post('p_price');
                $p_currency_type = $this->input->post('p_currency_type');

                // $p_img1 = base_url('./assets/postimg/'.$this->upload->data('raw_name').$this->upload->data('file_ext'));
                // $p_img2 = base_url('./assets/postimg/'.$this->upload->data('raw_name').$this->upload->data('file_ext'));
                // $p_img3 = base_url('./assets/postimg/'.$this->upload->data('raw_name').$this->upload->data('file_ext'));
                // $p_img4 = base_url('./assets/postimg/'.$this->upload->data('raw_name').$this->upload->data('file_ext'));
                // $owner_ins = base_url('./assets/inspection/'.$this->upload->data('owner_ins').$this->upload->data('file_ext'));


                $p_category = $this->input->post('p_category');
                $p_menu = $this->input->post('p_menu');
                $p_country = $this->input->post('p_country');
                $p_state = $this->input->post('p_state');
                $p_city = $this->input->post('p_city');
                $p_zipcode = $this->input->post('p_zipcode');
                $p_description = $this->input->post('p_description');
                $seo = $this->input->post('seo');
                $tags = $this->input->post('tags');
                $user_id = $this->input->post('user_id');
                $posted_date = date('l jS \of F Y h:i A');
                $status = "Active";

                $rental_store = $this->input->post('rental_store');
                $parts_store = $this->input->post('parts_store');


                    $store_id = $this->input->post('store_id');


                    $data = array(

                        'p_name' => $p_name,
                        'p_price' => $p_price,
                        'p_currency_type' => $p_currency_type,
                        // 'p_img1' => $p_img1,
                        // 'p_img2' => $p_img2,
                        // 'p_img3' => $p_img3,
                        // 'p_img4' => $p_img4,

                        // 'p_img1' => $dataInfo[0],
                  //       'p_img2' => $dataInfo[1],
                  //       'p_img3' => $dataInfo[2],
                  //       'p_img4' => $dataInfo[3],

                        'p_category' => $p_category,
                        'p_menu' => $p_menu,
                        'p_country' => $p_country,
                        'p_state' => $p_state,
                        'p_city' => $p_city,
                        'p_zipcode' => $p_zipcode,
                        'p_description' => $p_description,
                        'user_id' => $user_id,
                        'store_id'=>$store_id,
                        'posted_date'=>$posted_date,
                        'status'=>$status,
                        'seo'=>$seo,
                        'tags'=>$tags,



                    );


                 $this->usermodel->add_post($data);

                 $fetch_store_posts_col = $this->storesmodel->fetch_store_posts_col($store_id);

                    foreach ($fetch_store_posts_col as $store_posts) {
                        //
                        $total_posts = $store_posts->posts;

                    }

                    $pluspost = $total_posts+1;

                    $newdata = array(

                        'posts'=>$pluspost

                    );

                 $this->storesmodel->store_post_count($store_id,$newdata);
                 $cate_posts = $this->usermodel->fetch_cate_for_update($p_category = $this->input->post('p_category'));
                 $update_cate_posts['posts'] = $cate_posts+1;
                 $this->usermodel->update_categories($p_category = $this->input->post('p_category'),$update_cate_posts);


                 redirect('user/add_post');




            }else{
                // if validation not OK then show error
                $cates = $this->adminmodel->categories();
                $country = $this->adminmodel->fetch_country();
                $state = $this->adminmodel->fetch_state();
                $city = $this->adminmodel->fetch_city();
            $zipcode = $this->adminmodel->fetch_zipcode();

                $userdata = $this->usermodel->userdata();


                foreach ($userdata as $user ) {
                    //
                    $owner_id = $user->user_id;
                }

                $store_count = $this->usermodel->store_count($owner_id);

                $upload_error = "error";//array('error' => $this->upload->display_errors());

                $this->load->view('user/add_post',['cates'=>$cates,'country'=>$country,'state'=>$state,'city'=>$city,'zipcode'=>$zipcode,'userdata'=>$userdata,'store_count'=>$store_count],$upload_error);
            }



            }

    }

1 个答案:

答案 0 :(得分:0)

您正在错误地存储已保存的图像。你刚刚覆盖了以前的值而不是添加新值。

通过将所有图像上传并准备好插入数据库后将其代码放在for循环外,修复了将图像保存到数据库的问题。

添加帖子记录会发生两次。删除了第一个不需要的东西。

更新代码:

<?php
    public function publish_post(){
            // publish post
                if ( $this->input->post('add_post') != '' ) {
                    $this->form_validation->set_rules('p_name', 'Product Name','trim|required');
                $this->form_validation->set_rules('p_price', 'Product Price','trim|required');
                $this->form_validation->set_rules('p_currency_type', 'Currency Type');
                $this->form_validation->set_rules('p_category','Category','required');
                $this->form_validation->set_rules('p_menu','Menu','required');
                $this->form_validation->set_rules('p_country','Country','required');
                $this->form_validation->set_rules('p_state','State','required');
                $this->form_validation->set_rules('p_city','City','required');
                $this->form_validation->set_rules('p_zipcode','Zip Code','required');
                $this->form_validation->set_rules('p_description','Product Description','trim|required');
                // $config = ['upload_path' => './assets/postimg','allowed_types' => 'jpg|png|jpeg'];
                    //$this->load->library('upload',$config);

                if ( $this->form_validation->run() /*&& $this->upload->do_upload('p_img1') && $this->upload->do_upload('p_img2') && $this->upload->do_upload('p_img3') && $this->upload->do_upload('p_img4') */) {
                    // if validation OK then run this code
                    $config = ['upload_path' => './assets/postimg','allowed_types' => 'jpg|png|jpeg'];
                    $this->load->library('upload');
                    $dataInfo = array();
                    $files = $_FILES;
                    $cpt = count($_FILES['userfile']['name']);

            for($i=0; $i<$cpt; $i++){           
                $_FILES['userfile']['name']= $files['userfile']['name'][$i];
                $_FILES['userfile']['type']= $files['userfile']['type'][$i];
                $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
                $_FILES['userfile']['error']= $files['userfile']['error'][$i];
                $_FILES['userfile']['size']= $files['userfile']['size'][$i];    

                $this->upload->initialize($config);
                $this->upload->do_upload();
                $dataInfo['userfile'][] = $_FILES['userfile']['name'];    
            }

            $files = array(
                        'p_img1' => isset($dataInfo['userfile'][0])?$dataInfo['userfile'][0]:'',
                        'p_img2' => isset($dataInfo['userfile'][1])?$dataInfo['userfile'][1]:'',
                        'p_img3' => isset($dataInfo['userfile'][2])?$dataInfo['userfile'][2]:'',
                        'p_img4' => isset($dataInfo['userfile'][3])?$dataInfo['userfile'][3]:'',
                        'owner_ins'=>isset($dataInfo['userfile'][4])?$dataInfo['userfile'][4]:''
                );

        //$this->usermodel->add_post($files);

                    $p_name = $this->input->post('p_name');
                    $p_price = $this->input->post('p_price');
                    $p_currency_type = $this->input->post('p_currency_type');

                    // $p_img1 = base_url('./assets/postimg/'.$this->upload->data('raw_name').$this->upload->data('file_ext'));
                    // $p_img2 = base_url('./assets/postimg/'.$this->upload->data('raw_name').$this->upload->data('file_ext'));
                    // $p_img3 = base_url('./assets/postimg/'.$this->upload->data('raw_name').$this->upload->data('file_ext'));
                    // $p_img4 = base_url('./assets/postimg/'.$this->upload->data('raw_name').$this->upload->data('file_ext'));
                    // $owner_ins = base_url('./assets/inspection/'.$this->upload->data('owner_ins').$this->upload->data('file_ext'));


                    $p_category = $this->input->post('p_category');
                    $p_menu = $this->input->post('p_menu');
                    $p_country = $this->input->post('p_country');
                    $p_state = $this->input->post('p_state');
                    $p_city = $this->input->post('p_city');
                    $p_zipcode = $this->input->post('p_zipcode');
                    $p_description = $this->input->post('p_description');
                    $seo = $this->input->post('seo');
                    $tags = $this->input->post('tags');
                    $user_id = $this->input->post('user_id');
                    $posted_date = date('l jS \of F Y h:i A');
                    $status = "Active";

                    $rental_store = $this->input->post('rental_store');
                    $parts_store = $this->input->post('parts_store');


                        $store_id = $this->input->post('store_id');


                        $data = array(

                            'p_name' => $p_name,
                            'p_price' => $p_price,
                            'p_currency_type' => $p_currency_type,
                            // 'p_img1' => $p_img1,
                            // 'p_img2' => $p_img2,
                            // 'p_img3' => $p_img3,
                            // 'p_img4' => $p_img4,

                             'p_img1' => isset($dataInfo['userfile'][0])?$dataInfo['userfile'][0]:'',
                             'p_img2' => isset($dataInfo['userfile'][1])?$dataInfo['userfile'][1]:'',
                             'p_img3' => isset($dataInfo['userfile'][2])?$dataInfo['userfile'][2]:'',
                             'p_img4' => isset($dataInfo['userfile'][3])?$dataInfo['userfile'][3]:'',

                            'p_category' => $p_category,
                            'p_menu' => $p_menu,
                            'p_country' => $p_country,
                            'p_state' => $p_state,
                            'p_city' => $p_city,
                            'p_zipcode' => $p_zipcode,
                            'p_description' => $p_description,
                            'user_id' => $user_id,
                            'store_id'=>$store_id,
                            'posted_date'=>$posted_date,
                            'status'=>$status,
                            'seo'=>$seo,
                            'tags'=>$tags,

                        );

                     $this->usermodel->add_post($data);
                     $fetch_store_posts_col = $this->storesmodel->fetch_store_posts_col($store_id);

                        foreach ($fetch_store_posts_col as $store_posts) {
                            //
                            $total_posts = $store_posts->posts;

                        }

                        $pluspost = $total_posts+1;

                        $newdata = array(

                            'posts'=>$pluspost

                        );

                     $this->storesmodel->store_post_count($store_id,$newdata);
                     $cate_posts = $this->usermodel->fetch_cate_for_update($p_category = $this->input->post('p_category'));
                     $update_cate_posts['posts'] = $cate_posts+1;
                     $this->usermodel->update_categories($p_category = $this->input->post('p_category'),$update_cate_posts);    
                     redirect('user/add_post');

                }else{
                    // if validation not OK then show error
                    $cates = $this->adminmodel->categories();
                    $country = $this->adminmodel->fetch_country();
                    $state = $this->adminmodel->fetch_state();
                    $city = $this->adminmodel->fetch_city();
                    $zipcode = $this->adminmodel->fetch_zipcode();

                    $userdata = $this->usermodel->userdata();

                    foreach ($userdata as $user ) {
                        $owner_id = $user->user_id;
                    }

                    $store_count = $this->usermodel->store_count($owner_id);
                    $upload_error = "error";//array('error' => $this->upload->display_errors());

                    $this->load->view('user/add_post',['cates'=>$cates,'country'=>$country,'state'=>$state,'city'=>$city,'zipcode'=>$zipcode,'userdata'=>$userdata,'store_count'=>$store_count],$upload_error);
                }

               }
        }

        ?>