使用codeigniter

时间:2018-02-16 07:30:05

标签: php

**我遇到了更新图片的问题。我上传了多个图像并插入数据库但我无法覆盖数据库或我的上传文件夹中的这些图像如何覆盖我的上传文件夹和我的数据库2中的图像 投票 喜爱 4

我在代码点火器中执行多个图像上传相同的代码用于更新多个图像,但是在更新时间**

public function edit_package($ package_id){

    $pname      = $this->input->post('pname'); 
    $pdur       = $this->input->post('pdur');
    $pdelprice  = $this->input->post('pdelprice');
    $price      = $this->input->post('price');
    $pcat       = $this->input->post('pcat');
    $pc         = $this->input->post('pc'); 
    $ploc       = $this->input->post('ploc');
    $pcat       = $this->input->post('pcat');
    $accom1     = $this->input->post('accom1');
    $accom2     = $this->input->post('accom2');
    $accom3     = $this->input->post('accom3');
    $accom4     = $this->input->post('accom4');
    $accom5     = $this->input->post('accom5');
    $accom6     = $this->input->post('accom6');

    // Displaying Errors in view

    $this->form_validation->set_error_delimiters('<span style="color:red;">', '</span>');

    $this->form_validation->set_rules('pname', 'Package Name', 'required');

    $this->form_validation->set_rules('pdur', 'Package Duration', 'required');

    $this->form_validation->set_rules('price', 'Package Price', 'required');

    $this->form_validation->set_rules('pdelprice', 'Package offer Price', 'required');

    $this->form_validation->set_rules('pc', 'Places Covered', 'required');

    $this->form_validation->set_rules('pcat', 'Package Category', 'required');

    $this->form_validation->set_rules('ploc', 'Package Location', 'required');


    if ($this->form_validation->run() == False)
    {                                                              
        $data['editPackage']  = $this->Packages_model->editPackageVlaues($package_id);
        $data['editPackage']  = $this->Packages_model->editPackageVlaues($package_id);
        $data['editLocation']  = $this->Packages_model->edit_location();

        $data['editPackCate'] = $this->Packages_model->getCategoryName();

        $data['pkg_cat']      = $this->Packages_model->get_pkg_cat();
        $data['pkg_loc']      = $this->Api_location_model->get_locations();

        $this->load->view('Admin_panel/admin_assets');
        $this->load->view('Admin_panel/panel');
        $this->load->view('Admin_panel/Packages/edit_package',$data);
        $this->load->view('Admin_panel/footer');
    }

         extract($_POST);
           if(isset($update_package))
            {
                $filesCount = count($_FILES['pkg_img']['name']);

                for($i = 0; $i < $filesCount; $i++)
                 {

                    $_FILES['userFile']['name']     = $_FILES['pkg_img']['name'][$i];
                    $_FILES['userFile']['type']     = $_FILES['pkg_img']['type'][$i];
                    $_FILES['userFile']['tmp_name'] = $_FILES['pkg_img']['tmp_name'][$i];
                    $_FILES['userFile']['error']    = $_FILES['pkg_img']['error'][$i];
                    $_FILES['userFile']['size']     = $_FILES['pkg_img']['size'][$i];


                    $config['upload_path']          = 'resources/neeraj/';
                    $config['allowed_types']        = 'gif|jpg|png|jpeg|GIF|JPEG|PNG|JPG';
                    $config['overwrite']            = TRUE;
                    $config['max_size']             = 2048000;
                    $config['max_width']            = 1024;
                    $config['max_height']           = 768;

                    $this->upload->initialize($config);
                    if($this->upload->do_upload())
                    {
                        $fileData[] = $this->upload->data();
                        $fileName   = $fileData['file_name'];
                        $images[]   = $fileName;

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

                        $fileName = $this->upload->data('file_name');

                        if($this->Packages_model->update_packages($package_id,$pname,$pdur,$pdelprice,$price,$pcat,$ploc,$pc,$accom1,$accom2,$accom3,$accom4,$accom5,$accom6,$fileName))
                            {
                                $this->session->set_flashdata('package_update','Package Updated successfully');

                                redirect('Packages_ctrl/display_packages');
                            }

                            else
                            {
                                //upload the new image

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

                                $fileName = $upload_data['file_name'];

                                     if($_POST){

                                         $data = array('pkg_img'=>$fileName);

                                             //update
                                        $this->Packages_model->update_packages($data);
                                     }

                                echo "<script> alert('Please Select the Hotel Image');
                                                window.location.href='add_packages';
                                      </script>";

                            }                   
                    }
                 }
    }

    }

2 个答案:

答案 0 :(得分:0)

您应该提供覆盖config参数

$config['upload_path'] = './uploads/';
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
$this->upload->initialize($config);

请参阅上传首选项的文档

答案 1 :(得分:0)

我认为你想使用CodeIgniter上传多个文件。如果我是对的那你必须遵循这个答案,那么你肯定得到了解决方案。

在视图页面中

<form action='index.php/upload/do_upload' enctype="multipart/form-data" method="post">  
   <input type="file" name="userfile[]" multiple='multiple' />
   <input type="submit" value="upload" />  
</form>

在控制器中。

public function do_upload()
{
    $config['upload_path']          = './uploads/';
    $config['allowed_types']        = 'gif|jpg|png';
    $config['max_size']             = 10000;
    $config['max_width']            = 1024;
    $config['max_height']           = 768;
    $config['overwrite']            =  true;

    $this->load->library('upload', $config);

    $number_of_files_uploaded = count($_FILES['userfile']['name']);

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

       if ( ! $this->upload->do_upload('upl_files'))
       {
          $error = array('error' => $this->upload->display_errors());
          var_dump($error);
          $this->load->view('v_upload', $error);
       }
       else
       {
          $data = array('upload_data' => $this->upload->data());

          var_dump( $data);
       }    
    }  
 }
  

请关注这个名字,这是重要的一部分。