猿问

在Codeigniter中上传多个文件

我想使用单个元素上传多个文件。所以我尝试这个例子。


使用CodeIgniter 2.0上传多个文件(数组)


这是我的表格


<form enctype="multipart/form-data" class="jNice" accept-charset="utf-8" method="post" action="http://xxxx.dev/admin/add_estates">              

    <fieldset>      

            <label>Title * : </label>                       

            <input type="text" class="text-long" value="" name="title">


            <label>Description : </label>                       

            <textarea class="mceEditor" rows="10" cols="40" name="description"></textarea>


            <label>Image : </label>                     

            <input type="file" multiple="" name="images[]">                             


            <button class="button-submit" type="submit" name="save" id="">Save</button>

    </fieldset>         

</form>

这是我的控制器


public function add_estates()

{

    $data['page_title'] = "&copy; IDEAL - Administrator - Real State - Add Real Estate";

    $data['main_content'] = 'admin/add_estates';


    if ($this->input->post() !== FALSE) {           

        $this->load->library('form_validation');

        $this->form_validation->set_rules('title', 'Career Title', 'trim|required');           


        if ($this->form_validation->run() !== FALSE) {                


            $title = $this->input->post('title');

            $description = $this->input->post('description');


            if (!empty($_FILES['images']['name'][0])) {

                if ($this->upload_files($title, $_FILES['images']) === FALSE) {

                    $data['error'] = $this->upload->display_errors('<div class="alert alert-danger">', '</div>');

                }

            }                   


            if (!isset($data['error'])) {

                $this->admin_model->add_estate($title, $description, $image_name);    

                $this->session->set_flashdata('suc_msg', 'New real estate added successfully'); 

                redirect('admin/add_estates');    

            }          

        }

    }


但它给You did not select a file to upload.每一次。有什么问题


哈士奇WWW
浏览 385回答 3
3回答

守着一只汪

我images[]根据@Denmark 更改上传方法。&nbsp; &nbsp; private function upload_files($path, $title, $files)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $config = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'upload_path'&nbsp; &nbsp;=> $path,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'allowed_types' => 'jpg|gif|png',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'overwrite'&nbsp; &nbsp; &nbsp;=> 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; $this->load->library('upload', $config);&nbsp; &nbsp; &nbsp; &nbsp; $images = array();&nbsp; &nbsp; &nbsp; &nbsp; foreach ($files['name'] as $key => $image) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_FILES['images[]']['name']= $files['name'][$key];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_FILES['images[]']['type']= $files['type'][$key];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_FILES['images[]']['tmp_name']= $files['tmp_name'][$key];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_FILES['images[]']['error']= $files['error'][$key];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_FILES['images[]']['size']= $files['size'][$key];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $fileName = $title .'_'. $image;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $images[] = $fileName;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $config['file_name'] = $fileName;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->upload->initialize($config);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($this->upload->do_upload('images[]')) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->upload->data();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return $images;&nbsp; &nbsp; }

慕后森

private function upload_files($path, $title, $files){&nbsp; &nbsp; $config = array(&nbsp; &nbsp; &nbsp; &nbsp; 'upload_path'&nbsp; &nbsp;=> $path,&nbsp; &nbsp; &nbsp; &nbsp; 'allowed_types' => 'jpg|gif|png',&nbsp; &nbsp; &nbsp; &nbsp; 'overwrite'&nbsp; &nbsp; &nbsp;=> 1,&nbsp; &nbsp; );&nbsp; &nbsp; $this->load->library('upload', $config);&nbsp; &nbsp; $images = array();&nbsp; &nbsp; foreach ($files['name'] as $key => $image) {&nbsp; &nbsp; &nbsp; &nbsp; $_FILES['images[]']['name']= $files['name'][$key];&nbsp; &nbsp; &nbsp; &nbsp; $_FILES['images[]']['type']= $files['type'][$key];&nbsp; &nbsp; &nbsp; &nbsp; $_FILES['images[]']['tmp_name']= $files['tmp_name'][$key];&nbsp; &nbsp; &nbsp; &nbsp; $_FILES['images[]']['error']= $files['error'][$key];&nbsp; &nbsp; &nbsp; &nbsp; $_FILES['images[]']['size']= $files['size'][$key];&nbsp; &nbsp; &nbsp; &nbsp; $fileName = $title .'_'. $image;&nbsp; &nbsp; &nbsp; &nbsp; $images[] = $fileName;&nbsp; &nbsp; &nbsp; &nbsp; $config['file_name'] = $fileName;&nbsp; &nbsp; &nbsp; &nbsp; $this->upload->initialize($config);&nbsp; &nbsp; &nbsp; &nbsp; if ($this->upload->do_upload('images[]')) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->upload->data();&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return true;}
随时随地看视频慕课网APP
我要回答