【问题标题】:Codeigniter Upload multiple files different path to databaseCodeigniter 将不同路径的多个文件上传到数据库
【发布时间】:2014-02-27 08:45:08
【问题描述】:

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

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

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

控制器

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

【问题讨论】:

标签: php codeigniter


【解决方案1】:

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

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

【讨论】:

  • 你不需要多次加载库,你可以在同一个库上调用initialize,它会切换配置...
  • 完全正确,完美。即使在 codeigniter 文档中我也找不到这个。非常感谢!
【解决方案2】:
$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);
            }
            }

【讨论】:

    猜你喜欢
    • 2011-04-08
    • 2018-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-21
    • 2012-06-09
    相关资源
    最近更新 更多