How to insert 1 image in 2 different directories using codeigniter

安稳与你 提交于 2020-01-26 03:50:07

问题


I am trying to insert 1 image in 2 different directories but it is saving only in the first directory but not in the second directory.I am unable find the mistake in my code,please check the below code and help us.Thanks in advance.

public function image()
{ 
    $data = array();
    $error = array();
    $config1=array(
        'upload_path'=>'upload/',
        'allowed_types'=>'jpg|jpeg|png|bmp',
        'max_size'=>0,
        'filename'=>url_title($this->input->post('file'))
    );

    $this->load->library('upload',$config1);
    if($this->upload->do_upload('file')){
        $error = array('error' => $this->upload->display_errors());
        echo "<pre>";
        print_r($error);
        exit();
    }else {
        $fdata = $this->upload->data();
        $data['image'] = 'upload/' . $fdata['file_name'];
    }

    $config2=array(
        'upload_path'=>'upload/images/',
        'allowed_types'=>'jpg|jpeg|png|bmp',
        'max_size'=>0,
        'filename'=>url_title($this->input->post('file'))
    );
    $this->upload->initialize($config2);

    if (!$this->upload->do_upload('file')){
        $error = array('error' => $this->upload->display_errors());
        echo "<pre>";
        print_r($error);
        exit();
    } else {
        $fdata = $this->upload->data();
        $data['file2'] = 'upload/images' . $fdata['file_name'];
    }

    $data['name'] = $this->input->post('dname', TRUE);
    $data['email'] = $this->input->post('demail', TRUE);
    $data['mobile'] = $this->input->post('dmobile', TRUE);
    $data['mobile'] = $this->input->post('daddress', TRUE);

    $result = $this->insert_model->insert($data);
    $sdata = array();
    $sdata['message'] = "Well done!</strong> You successfully add the 
    Product Details.";
    $this->session->set_userdata($sdata);
    redirect('super_admin/add_product', 'refresh');
}

回答1:


You should make a method for upload, so you can use it more times.

    /**
     * Upload a file.
     * 
     * @param $field
     * @param $upload_path
     * @param string $allowed_types
     * @return bool | file_name
     */
    public function do_upload($field, $upload_path, $allowed_types = 'jpg|png|gif')
    {
        $config['upload_path']          = $upload_path;
        $config['allowed_types']        = $allowed_types;
        $config['max_size']             = 500;
        $config['max_width']            = 1024;
        $config['max_height']           = 1024;

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

        if ( ! $this->upload->do_upload($field))
        {
            $this->upload = null; // Unload library
            return FALSE;
        }
        else
        {
            $data = $this->upload->data();
            $this->upload = null; // Unload library
            return $data["file_name"];
        }
    }

Now you can use it in image().

Example

public function image()
{
    $data = array();

    // For 1st directory
    if ($data['image'] = $this->do_upload('your_filed_name', 'upload/')) {
       // Statements
        // $data['image'] = 'upload/' . $data['image']
    } else {
        $data['errors'] = array('error' => $this->upload->display_errors());
    }

    // For 2st directory
    if ($data['image'] = $this->do_upload('your_filed_name', 'upload/images/')) {
        // Statements
        // $data['image2'] = 'upload/images/' . $data['image']
    } else {
        $data['errors'] = array('error' => $this->upload->display_errors());
    }

    // Your rest of logic
}



回答2:


Try this

public function image()
{ 
    $data = array();
    $error = array();
    $config1=array(
        'upload_path'=>'upload/',
        'allowed_types'=>'jpg|jpeg|png|bmp',
        'max_size'=>0,
        'filename'=>url_title($this->input->post('file'))
    );

    $this->load->library('upload',$config1,'file1'); // Create custom object for file 1 upload
    if($this->file1->do_upload('file')){
        $error = array('error' => $this->file1->display_errors());
        echo "<pre>";
        print_r($error);
        exit();
    }else {
        $fdata = $this->file1->data();
        $data['image'] = 'upload/' . $fdata['file_name'];
    }

    $config2=array(
        'upload_path'=>'upload/images/',
        'allowed_types'=>'jpg|jpeg|png|bmp',
        'max_size'=>0,
        'filename'=>url_title($this->input->post('file'))
    );


    $this->load->library('upload', $config2, 'file2'); // Create custom object for file 2 upload
    $this->file2->initialize($config2);
    if (!$this->file2->do_upload('file')){
        $error = array('error' => $this->file2->display_errors());
        echo "<pre>";
        print_r($error);
        exit();
    } else {
        $fdata = $this->file2->data();
        $data['file2'] = 'upload/images' . $fdata['file_name'];
    }

    $data['name'] = $this->input->post('dname', TRUE);
    $data['email'] = $this->input->post('demail', TRUE);
    $data['mobile'] = $this->input->post('dmobile', TRUE);
    $data['mobile'] = $this->input->post('daddress', TRUE);

    $result = $this->insert_model->insert($data);
    $sdata = array();
    $sdata['message'] = "Well done!</strong> You successfully add the 
    Product Details.";
    $this->session->set_userdata($sdata);
    redirect('super_admin/add_product', 'refresh');
}



回答3:


My problem resolved with following code:

    public function image()
    {    

            $config['upload_path']          = './upload/images';
            $config['allowed_types']        = 'gif|jpg|png';
            $config['max_size']             = 0;
         $this->load->library('upload', $config);
    if (!$this->upload->do_upload('userfile')){
       } else {
       $fdata = $this->upload->data();
    $data['file2'] = 'upload/images' . $fdata['file_name'];
       } 
            $config2=array(
    'upload_path'=>'upload/',
    'allowed_types'=>'jpg|jpeg|png|bmp',
    'max_size'=>0,
     );
     $this->upload->initialize($config2);

    if($this->upload->do_upload('userfile')){
   $data = array(
    'name' =>$this->input->post('name'),
   'email' =>$this->input->post('email'),
   'phone' =>$this->input->post('phone'),
   'title' =>$this->input->post('someField'),
   'image'=>$this->upload->file_name,
    );
    //Transfering data to Model
    $this->insert_model->add($data);
            }
            else
            {
                    $data = array('upload_data' => $this->upload->data());

                    $this->load->view('upload_success', $data);
            }
     }
     }


来源:https://stackoverflow.com/questions/47327901/how-to-insert-1-image-in-2-different-directories-using-codeigniter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!