简体   繁体   中英

How to insert 1 image in 2 different directories using codeigniter

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

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
}

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM