dongtangxi1584 2014-02-24 16:49
浏览 40

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

I need some help. I want to upload two different files in two different directory by using 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');
    }
}
  • 写回答

2条回答 默认 最新

  • duanchen7036 2014-02-24 17:04
    关注

    First, loading the library again with $config2 won't work because the library is already loaded once and $config1 will stay loaded. To load a new config use $this->upload->initialize($config2);

    Second, loading $config2 will overwrite the previous config. You should re-arrange your code. Otherwise both uploads will only use the latest config ($config2). Example:

    1. Load library with $config1
    2. Process do_upload('product_small_image') and collect result
    3. Load $confg2 using initialize()
    4. Process do_upload('product_large_image') and collect result
    5. Process results (save to db if success or display error if one of the uploads failed)
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口