Codeigniter:如何上传不同的文件(图像和文件)

时间:2013-12-17 06:44:38

标签: php 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);
        }
    }

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

请帮忙。谢谢

2 个答案:

答案 0 :(得分: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;
                }
            }

答案 1 :(得分:1)

使用 $ this-&gt; upload-&gt; 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