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

Php 如何通过codeigniter在两个不同的目录中上传两个不同的文件?,php,codeigniter,Php,Codeigniter,我需要一些帮助。我想使用codeigniter在两个不同的目录中上传两个不同的文件。我在控制器中编写了以下代码。但它只会上传第一张图片 public function save_product() { $data = array(); $error = array(); $config1['upload_path'] = './manager/images/products/'; $config1['allowed_types'] = 'gif|jpeg|png|

我需要一些帮助。我想使用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');
    }
}
公共功能保存_产品(){
$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);
如果(!$this->upload->do_upload('product_small_image'))和(!$this->upload->do_upload('product_large_image')){
$error=array('error'=>$this->upload->display_errors());
回声“;
打印错误($error);
退出();
}
否则{
$fdata=$this->upload->data();
$data['product\u small\u image']='manager/images/products/'。$fdata['file\u name'];
$data['product\u large\u image']='manager/images/products/large/'。$fdata['file\u name'];
$data['product\u id']=$this->input->post('product\u id',TRUE);
$data['product\u name']=$this->input->post('product\u name',TRUE);
$data['category']=$this->input->post('category',TRUE);
$result=$this->super\u admin\u model->save\u product\u detail($data);
$sdata=array();
$sdata['message']=“干得好!您已成功添加产品详细信息。”;
$this->session->set_userdata($sdata);
重定向(“超级管理员/添加产品”、“刷新”);
}
}

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

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

  • 使用$config1加载库
  • 处理do_上传(“产品小图像”)并收集结果
  • 使用initialize()加载$confg2
  • 处理do_上传(“产品大图像”)并收集结果
  • 处理结果(如果成功,则保存到db;如果其中一个上载失败,则显示错误)

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

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

  • 使用$config1加载库
  • 处理do_上传(“产品小图像”)并收集结果
  • 使用initialize()加载$confg2
  • 处理do_上传(“产品大图像”)并收集结果
  • 处理结果(如果成功,则保存到db;如果其中一个上载失败,则显示错误)

  • 下面是我为成功上传不同目录中的多个图像而编写的全部代码。再次感谢@Samutz

    公共功能保存_产品(){
    $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);
    如果(!$this->upload->do_upload('product_small_image')){
    $error=array('error'=>$this->upload->display_errors());
    回声“;
    打印错误($error);
    退出();
    }
    否则{
    $fdata=$this->upload->data();
    $data['product\u small\u image']='manager/images/products/'。$fdata['file\u 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);
    如果(!$this->upload->do_upload('product_large_image')){
    $error=array('error'=>$this->upload->display_errors());
    回声“;
    打印错误($error);
    退出();
    }
    否则{
    $fdata=$this->upload->data();
    $data['product\u large\u image']='manager/images/products/large/'。$fdata['file\u name'];
    }
    $data['product\u id']=$this->input->post('product\u id',TRUE);
    $data['product\u name']=$this->input->post('product\u name',TRUE);
    $data['category']=$this->input->post('category',TRUE);
    $result=$this->super\u admin\u model->save\u product\u detail($data);
    $sdata=array();
    $sdata['message']=“干得好!您已成功添加产品详细信息。”;
    $this->session->set_userdata($sdata);
    重定向(“超级管理员/添加产品”、“刷新”);
    }
    
    以下是我为成功将多个图像上传到不同目录而编写的全部代码。再次感谢@Samutz

    公共功能保存_产品(){
    $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);
    如果(!$this->upload->do_upload('product_small_image')){
    $error=array('error'=>$this->upload->display_errors());
    回声“;
    打印错误($error);
    退出();
    }
    否则{
    $fdata=$this->upload->data();
    $data['product\u small\u image']='manager/images/products/'。$fdata['file\u name'];
    }
    $config2['upload_path']='./manager/images/products/large/';
    $config2['allowed_types']='gif | jpeg | png | jpg';
    $config2['max_size']='3000000';
    $config2['max\u widt
    
    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');
    }