Codeigniter上传多个文件到数据库的不同路径

时间:2014-02-27 08:45:09

标签: php codeigniter

我在使用codeigniter进行多次上传时出现问题,

  1. 使用图片
  2. 另一个使用pdf
  3. 当我上传两次上传的文件以及如何调用上传到数据库的不同路径时。这是我的代码

    控制器

    public function upload(){
    
        $catalog='catalog';
        $userfile='userfile';
    
        //for cover
        $config['upload_path'] = './file/book/'; //Use relative or absolute path
        $config['allowed_types'] = 'gif|jpg|png|'; 
        $config['max_size'] = '100000';
        $config['max_width'] = '1024';
        $config['max_height'] = '768';
        $this->load->library('upload', $config);
        $this->upload->initialize($config); 
    
        //$c=$this->upload->do_upload($userfile);
    
        //for catalog
        $config['upload_path'] = './file/book/pdf/'; //Use relative or absolute path
        $config['allowed_types'] = 'pdf'; 
        $config['max_size'] = '1000000';
        $this->load->library('upload', $config);
        $this->upload->initialize($config);
    
        //$cat=$this->upload->do_upload($catalog);
    
    
        if(!$this->upload->do_upload($catalog) &&         $this->upload->do_upload($userfile)){
    
    
            $error = array('error' => $this->upload->display_errors());
            $this->load->view('uploadds', $error);
    
        }else{ 
    
    
    
            $this->load->model("book_model"); 
            $this->book_model->addnewbook();
            redirect('book/book');      
        }
    

    }

    此型号

    function addnewbook(){
         $fcat=array('upload_data' => $this->upload->data($userfile));
         $fcatalog=array('upload_dataa' => $this->upload->data($catalog));
    }
    

2 个答案:

答案 0 :(得分:5)

您需要独立处理多个上传。为此,您必须在加载上传库时为两个上传创建单独的自定义对象。 (见代码评论)

  public function upload() {

    // Cover upload
    $config = array();
    $config['upload_path'] = './file/book/';
    $config['allowed_types'] = 'gif|jpg|png|';
    $config['max_size'] = '100000';
    $config['max_width'] = '1024';
    $config['max_height'] = '768';
    $this->load->library('upload', $config, 'coverupload'); // Create custom object for cover upload
    $this->coverupload->initialize($config);
    $upload_cover = $this->coverupload->do_upload('cover');

    // Catalog upload
    $config = array();
    $config['upload_path'] = './file/book/pdf/';
    $config['allowed_types'] = 'pdf';
    $config['max_size'] = '1000000';
    $this->load->library('upload', $config, 'catalogupload');  // Create custom object for catalog upload
    $this->catalogupload->initialize($config);
    $upload_catalog = $this->catalogupload->do_upload('catalog');

    // Check uploads success
    if ($upload_cover && $upload_catalog) {

      // Both Upload Success

      // Data of your cover file
      $cover_data = $this->coverupload->data();
      print_r($cover_data);

      // Data of your catalog file
      $catlog_data = $this->catalogupload->data();          
      print_r($catlog_data);
    } else {

      // Error Occured in one of the uploads

      echo 'Cover upload Error : ' . $this->coverupload->display_errors() . '<br/>';
      echo 'Catlog upload Error : ' . $this->catalogupload->display_errors() . '<br/>';
    }
  }

使用$cover_data['full_path']$catlog_data['full_path']上的数据更新数据库

答案 1 :(得分:0)

$config['upload_path'] = 'frontend_assets/images/hospital';
$config['allowed_types'] = 'gif|jpg|png|jpeg|JPEG||JPG|PNG';
$this->load->library('upload', $config);

if($_FILES['logo']['name']){
$config['file_name'] = time() . $_FILES["logo"]['name'];

  if (!$this->upload->do_upload('logo')) {

    $this->upload->display_errors();

            } else {

                $upload = $this->upload->data();

                $insert_data['logo'] = $config['upload_path'] . '/' . $upload['file_name'];

            }
            }


if($_FILES['bulding_photo']['name']){
                 $config['file_name'] = time() . $_FILES["bulding_photo"]['name'];

            if (!$this->upload->do_upload('bulding_photo')) {

                $this->upload->display_errors());

            } else {

                $upload = $this->upload->data();

                $insert_data['bulding_photo'] = $config['upload_path'] . '/' . $upload['file_name'];
                $this->image_size_fix($insert_data['bulding_photo'], $width = 200, $height = 200);
            }
            }