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

时间:2014-02-24 16:49:03

标签: php codeigniter

我需要一些帮助。我想使用codeigniter在两个不同的目录中上传两个不同的文件。我在我的控制器中编写了以下代码。但它只会上传第一张图片。

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

2 个答案:

答案 0 :(得分:4)

首先,使用$ config2再次加载库将不起作用,因为库已经加载一次并且$ config1将保持加载状态。要加载新配置,请使用$this->upload->initialize($config2);

其次,加载$ config2将覆盖以前的配置。你应该重新安排你的代码。否则,两个上传只会使用最新的配置($ config2)。例如:

  1. 使用$ config1
  2. 加载库
  3. 处理do_upload('product_small_image')并收集结果
  4. 使用initialize()
  5. 加载$ confg2
  6. 处理do_upload('product_large_image')并收集结果
  7. 处理结果(如果成功则保存为db,如果其中一个上载失败,则显示错误)

答案 1 :(得分:0)

以下是我编写的用于在不同目录中成功上传多个图像的总代码。并再次感谢@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');
}