【问题标题】:Codeigniter: How to upload different files (images & documents)Codeigniter:如何上传不同的文件(图像和文档)
【发布时间】:2014-01-04 19:56:53
【问题描述】:

我是codeigniter的新手,请你帮我如何在codeigniter中上传多个文件(文档和图像)。

这是我的示例代码

查看代码

<label>Photo</label>
   <input type="file" name="employee_photo" /> 
   <label>Document</label>
   <input type="file" name="employee_doc" />

控制器代码

function insertEmployee()
    {
       $query = $this->employee_model->insertEmployee();

        if($query) {                
             $this->do_upload_image('employee_photo');
             $this->do_upload_file('employee_doc');
        }
     }

    function do_upload_image($field_name) {

        $config['upload_path'] = './uploads/';
        $config['upload_url']  = base_url()."uploads/";                
        $config['allowed_types'] = "gif|jpg|png|jpeg";          
        $config['overwrite'] = TRUE;$config['max_size'] = '1000KB';
        $config['max_width']  = '1024';             
        $config['max_height']  = '768';
        $this->load->library('upload', $config);

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

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


    }
    function do_upload_file($field_name) {
        $config_file['upload_path'] = './uploads/';
        $config_file['upload_url']  = base_url()."uploads/";
        $config_file['allowed_types'] = "pdf|doc|docx";               
        $config_file['encrypt_name'] = true;            
        $config_file['overwrite'] = TRUE;           
        $config_file['max_size'] = '1000KB';

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

        if ( ! $this->upload->do_upload($field_name)) {
           $error = array('error' => $this->upload->display_errors());
           $this->load->view('add_employee', $error);           
        } else {
           $data_file = array('upload_data_file' => $this->upload->data());
           $this->load->view('employee_listing', $data_file);
        }
    }

我尝试了谷歌,但我没有看到任何解决方案。

请帮忙。谢谢

【问题讨论】:

标签: php codeigniter


【解决方案1】:

我刚刚发现解决上传不同的文件(图像和文档)。

function multiple_upload($upload_dir = 'uploads', $config = array())
            {
                $files = array();

                if(empty($config))
                {
                    $config['upload_path'] = './uploads/';
                    $config['allowed_types'] = 'gif|jpg|jpeg|jpe|png|pdf|doc|docx';
                    $config['max_size']      = '800000000';
                }

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

                $errors = FALSE;

                foreach($_FILES as $key => $value)
                {            
                    if( ! empty($value['name']))
                    {
                        if( ! $this->upload->do_upload($key))
                        {                                           
                            $data['upload_message'] = $this->upload->display_errors(ERR_OPEN, ERR_CLOSE); // ERR_OPEN and ERR_CLOSE are error delimiters defined in a config file
                            $this->load->vars($data);

                            $errors = TRUE;
                        }
                        else
                        {
                            // Build a file array from all uploaded files
                            $files[] = $this->upload->data();
                        }
                    }
                }

                // There was errors, we have to delete the uploaded files
                if($errors)
                {                    
                    foreach($files as $key => $file)
                    {
                        @unlink($file['full_path']);    
                    }                    
                }
                elseif(empty($files) AND empty($data['upload_message']))
                {
                    $this->lang->load('upload');
                    $data['upload_message'] = ERR_OPEN.$this->lang->line('upload_no_file_selected').ERR_CLOSE;
                    $this->load->vars($data);
                }
                else
                {
                    return $files;
                }
            }

【讨论】:

    【解决方案2】:

    使用$this->upload->initialize($config);

    // Save images
    $config['upload_path'] = 'assets/image/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['encrypt_name'] = TRUE;
    
    $this->load->library('upload', $config);
    $this->upload->initialize($config);
    
    $this->upload->do_upload('image'); //image is the name of your post file
    
    // Save document
    $config['upload_path'] = 'assets/documents/';
    $config['allowed_types'] = 'docx|rtf|doc|pdf';
    $config['encrypt_name'] = TRUE;
    
    $this->load->library('upload', $config);
    $this->upload->initialize($config);
    
    $this->upload->do_upload('document'); //document is the name of your post file
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多