在codeigniter 2.0中使用上传多文件时不显示错误

时间:2015-07-20 19:35:35

标签: codeigniter file-upload

我是codeigniter的新手。我在codeigniter中上传多个文件时遇到问题。 我的上传表单可以上传多个文件,调整大小并保存到数据库,一切正常,只有一件事不行:不显示错误。示例:最大文件为2MB,我选择文件3MB,它不显示消息“您尝试上传的文件大于允许的大小”。单击按钮上传但不选择文件时出现相同的错误。视图将像图像一样显示为链接。 (我没有声望发布图片) https://drive.google.com/file/d/0B2zpQIbKaLa6Z2Z6LXFrNXlVRUk/view?usp=sharing

这是我的控制器:upload.php

private function _upload_files($field='userfile'){
    $files = array();
    foreach( $_FILES[$field] as $key => $all )
        foreach( $all as $i => $val )
            $files[$i][$key] = $val;

        $files_uploaded = array();
        for ($i=0; $i < count($files); $i++) { 
            $_FILES[$field] = $files[$i];
            if ($this->upload->do_upload($field))
                $files_uploaded[$i] = $this->upload->data($files);
            else
                //$files_uploaded[$i] = null;
                $files_uploaded[$i] = $this->upload->display_errors();
        }
        return $files_uploaded;
    }

    public function do_upload(){
        $data['error']="";
        $this->_data['loadPage']="upload/upload_view";
        $this->load->model('Mcategorie');
        $this->_data["categories"] = $this->Mcategorie->listAllCate();
        $this->load->model('Malbum');
        $this->_data['albums']=$this->Malbum->listAllAlbum1();
        $user_folder = './uploads/'.$this->session->userdata('username');
        $thumb_folder = './uploads/'.$this->session->userdata('username').'/thumbnail/';

        if(!is_dir($user_folder)){
            mkdir($user_folder, 0777);
        }
        if(is_dir($user_folder)){
            if(!is_dir($thumb_folder))
                mkdir($thumb_folder,0777);
        }

        if($this->input->post("ok")){
            $config['upload_path'] = $user_folder;
            $config['allowed_types'] = 'gif|jpg|png|jpeg';
            $config['max_size'] = '2048';
            $config['max_width']  = '';
            $config['max_height']  = '';
            $config['overwrite'] = FALSE;
            $config['remove_spaces'] = TRUE;
            $this->load->library("upload",$config);
            $this->load->library("image_lib");

            if ($_FILES['image_list']) {
                $images_upload= $this->_upload_files('image_list');
                //echo "<pre>";
                //print_r($images_upload);exit;
                foreach($images_upload as $data){

                    $config['image_library'] = 'gd2';
                    $config['source_image'] = './uploads/'.$this->session->userdata('username').'/'.$data['file_name'];
                    $config['create_thumb'] = TRUE; 
                    $config['new_image'] = './uploads/'.$this->session->userdata('username').'/thumbnail/'.$data['file_name'];
                    $config['maintain_ratio'] = FALSE;
                    $config['width']     = 400;
                    $config['height']   = 300;
                    $this->image_lib->initialize($config);
                    $this->image_lib->resize();

                    $file = array(
                        'name'          => $data['raw_name'].$data['file_ext'],
                        'thumb_name'    => 'thumb_'.$data['raw_name'].$data['file_ext'],
                        'date'          => date("Y-m-d"),
                        'userid'        => $this->session->userdata('userid'),
                        'username'      => $this->session->userdata('username'),
                        'description'   => $this->input->post("description"),
                        'roleid'        => $this->session->userdata("roleid"),
                        'public'        => $this->input->post("kiet"),
                        'albumid'       => $this->input->post("album"),
                        'categoryid'    => $this->input->post("cat"),
                        );
                    $this->Mupload->insert_images($file);
                }
                redirect(base_url()."default/user/profile","refresh");
            }
        }
        $this->load->view ( $this->_data['path'], $this->_data);
    }

这是我的观点:upload_view.php

 <div class="upload container col-xs-12 col-sm-12 col-md-12 col-lg-12">
<div class="panel panel-info">
  <div class="panel-heading"><strong>Upload Files</strong> <small>Sao Bac Dau Photo Sharing</small></div>
  <div class="panel-body">

    <!-- Standar Form -->

    <form action="<?php echo base_url()."default/upload/do_upload"?>" method="post" enctype="multipart/form-data">
      <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">
        <label> Chọn file ảnh cần upload (jpg, jpeg, gif, png) : </label>

        <div class="error col-xs-12 col-sm-12 col-md-12 col-lg-12 clear-css" style="color:#FF0081; font-weight: bold">
          <?php if(isset($error)){echo $error; }?>
        </div>
        <div class="input-group">
          <span class="input-group-btn" >
            <span class="btn btn-primary btn-file">
              Browse &hellip; <input type="file" multiple id="image_list" name="image_list[]" accept="image/*" >
            </span>
          </span>
          <input type="text" class="form-control" readonly>
        </div>
        <input type="submit" name="ok" class="btn btn-md btn-primary" value="Upload Photo"  >
      </div>
    </form>
  </div>
</div>

我该怎么做才能显示错误?对不起,如果我的英语太糟糕了。提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您将错误存储在 $ files_uploaded [$ i] = $ this-&gt; upload-&gt; display_errors(); 中,但不会使用此错误来显示错误,因此您需要从中获取错误这个数组并在视图上显示它。

请参阅以下代码。

   <?php
   class Upload extends CI_Controller {

        function __construct()
        {
                parent::__construct();
                $this->load->helper(array('form', 'url'));
        }

        function index()
        {
                $this->load->view('upload_form', array('error' => ' ' ));
        }

        function do_upload()
        {
                $config['upload_path'] = './uploads/';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size'] = '100';
                $config['max_width']  = '1024';
                $config['max_height']  = '768';

                $this->load->library('upload', $config);

                if ( ! $this->upload->do_upload())
                {
                        $error = array('error' => $this->upload->display_errors());

                        $this->load->view('upload_form', $error);
                }
                else
                {
                        $data = array('upload_data' => $this->upload->data());

                        $this->load->view('upload_success', $data);
                }
        }
   }
   ?>