如何使用codeigniter在2个不同的目录中插入1个图像

时间:2017-11-16 10:59:10

标签: php codeigniter

我正在尝试在2个不同的目录中插入1个图像,但它只保存在第一个目录中但不保存在第二个目录中。我无法在我的代码中找到错误,请检查以下代码并帮助我们。谢谢提前。

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

3 个答案:

答案 0 :(得分:1)

你应该制作一个上传方法,这样你就可以多次使用它。

    /**
     * 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"];
        }
    }

现在你可以在image()中使用它。

实施例

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
}

答案 1 :(得分:0)

试试这个

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

答案 2 :(得分:0)

我的问题通过以下代码解决:

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