简体   繁体   English

Codeigniter如何在两个不同的目录中上传两个不同的文件?

[英]How to upload two different files in two different directory by codeigniter?

I need some help. 我需要协助。 I want to upload two different files in two different directory by using codeigniter. 我想使用codeigniter在两个不同的目录中上载两个不同的文件。 I wrote the following code in my controller. 我在控制器中编写了以下代码。 but it will upload only the first image. 但只会上传第一张图片。

public function save_product() {
    $data = array();
    $error = array();
    $config1['upload_path'] = './manager/images/products/';
    $config1['allowed_types'] = 'gif|jpeg|png|jpg';
    $config1['max_size'] = '3000000';
    $config1['max_width'] = '1024';
    $config1['max_height'] = '768';
    $this->load->library('upload', $config1);
    $config2['upload_path'] = './manager/images/products/large/';
    $config2['allowed_types'] = 'gif|jpeg|png|jpg';
    $config2['max_size'] = '3000000';
    $config2['max_width'] = '1024';
    $config2['max_height'] = '768';
    $this->load->library('upload', $config2);

    if ((!$this->upload->do_upload('product_small_image')) && (!$this->upload->do_upload('product_large_image'))){
        $error = array('error' => $this->upload->display_errors());
        echo "<pre>";
        print_r($error);
        exit();
    }

    else {
        $fdata = $this->upload->data();
        $data['product_small_image'] = 'manager/images/products/' . $fdata['file_name'];
        $data['product_large_image'] = 'manager/images/products/large/' . $fdata['file_name'];
        $data['product_id'] = $this->input->post('product_id', TRUE);
        $data['product_name'] = $this->input->post('product_name', TRUE);
        $data['category'] = $this->input->post('category', TRUE);

        $result = $this->super_admin_model->save_product_detail($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');
    }
}

First, loading the library again with $config2 won't work because the library is already loaded once and $config1 will stay loaded. 首先,使用$ config2重新加载该库将不起作用,因为该库已经加载一次,并且$ config1将保持加载状态。 To load a new config use $this->upload->initialize($config2); 要加载新配置,请使用$this->upload->initialize($config2);

Second, loading $config2 will overwrite the previous config. 其次,加载$ config2将覆盖以前的配置。 You should re-arrange your code. 您应该重新排列代码。 Otherwise both uploads will only use the latest config ($config2). 否则,两个上传都将仅使用最新的配置($ config2)。 Example: 例:

  1. Load library with $config1 用$ config1加载库
  2. Process do_upload('product_small_image') and collect result 处理do_upload('product_small_image')并收集结果
  3. Load $confg2 using initialize() 使用initialize()加载$ confg2
  4. Process do_upload('product_large_image') and collect result 处理do_upload('product_large_image')并收集结果
  5. Process results (save to db if success or display error if one of the uploads failed) 处理结果(如果成功则保存到数据库,或者如果其中一个失败则显示错误)

Here is the total code what i wrote to upload multiple image in different directory successfully. 这是我编写的成功在不同目录中成功上传多个图像的总代码。 and thanks to @Samutz again. 并再次感谢@Samutz。

public function save_product() {
$data = array();
$error = array();
$config1['upload_path'] = './manager/images/products/';
$config1['allowed_types'] = 'gif|jpeg|png|jpg';
$config1['max_size'] = '3000000';
$config1['max_width'] = '1024';
$config1['max_height'] = '768';
$this->load->library('upload', $config1);

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

$config2['upload_path'] = './manager/images/products/large/';
$config2['allowed_types'] = 'gif|jpeg|png|jpg';
$config2['max_size'] = '3000000';
$config2['max_width'] = '1024';
$config2['max_height'] = '768';
$this->upload->initialize($config2);

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

    $data['product_id'] = $this->input->post('product_id', TRUE);
    $data['product_name'] = $this->input->post('product_name', TRUE);
    $data['category'] = $this->input->post('category', TRUE);

    $result = $this->super_admin_model->save_product_detail($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');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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