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

标签 php codeigniter

我在使用 codeigniter 进行多次上传时遇到问题,

  1. 使用图片
  2. 另一个使用 pdf

当我上传文件时上传了两次以及如何调用不同的路径上传到数据库。这是我的代码

Controller

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

最佳答案

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

  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'] 上的数据更新您的数据库

关于php - Codeigniter 将多个文件上传到数据库的不同路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22063256/

相关文章:

php - 获取非英语的用户输入

javascript - 在 AJAX 上更新号码,laravel 5.2

php - 如何从 PHP 扩展返回数组,而不将其复制到内存中?

php - 引用:什么是变量范围,哪些变量可从何处访问,什么是“ undefined variable ”错误?

php - Laravel:将参数传递给函数?

php - CodeIgniter View 页面为空

php - 如何在codeigniter中从动态表插入多个数据到mysql

file - 在 codeigniter 中使用上传类 - 模型还是 Controller ?

php - 如何使用 codeigniter 3 调用 hooks 文件夹中的库?

php - 集成 Braintree php 时出错