php - 在 Codeigniter 中上传多个文件

标签 php codeigniter file-upload upload

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

Multiple files upload (Array) with 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>

这是我的 Controller

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

    $data['suc_msg'] = $this->session->flashdata('suc_msg');

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

这是我的文件上传方式

private function upload_files($title, $files)
{
    $config = array(
        'upload_path'   => './upload/real_estate/',
        'allowed_types' => 'jpg|gif|png',
        'overwrite'     => 1,                       
    );

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

    foreach ($files['name'] as $key => $image) {
        $_FILES['images']['name']= $files['name'][$key];
        $_FILES['images']['type']= $files['type'][$key];
        $_FILES['images']['tmp_name']= $files['tmp_name'][$key];
        $_FILES['images']['error']= $files['error'][$key];
        $_FILES['images']['size']= $files['size'][$key];

        $config['file_name'] = $title .'_'. $image;

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

        if ($this->upload->do_upload($image)) {
            $this->upload->data();
        } else {
            return false;
        }
    }

    return true;
}

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

最佳答案

根据@Denmark,我用 images[] 改变了上传方法。

    private function upload_files($path, $title, $files)
    {
        $config = array(
            'upload_path'   => $path,
            'allowed_types' => 'jpg|gif|png',
            'overwrite'     => 1,                       
        );

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

        $images = array();

        foreach ($files['name'] as $key => $image) {
            $_FILES['images[]']['name']= $files['name'][$key];
            $_FILES['images[]']['type']= $files['type'][$key];
            $_FILES['images[]']['tmp_name']= $files['tmp_name'][$key];
            $_FILES['images[]']['error']= $files['error'][$key];
            $_FILES['images[]']['size']= $files['size'][$key];

            $fileName = $title .'_'. $image;

            $images[] = $fileName;

            $config['file_name'] = $fileName;

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

            if ($this->upload->do_upload('images[]')) {
                $this->upload->data();
            } else {
                return false;
            }
        }

        return $images;
    }

关于php - 在 Codeigniter 中上传多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20113832/

相关文章:

javascript - 使用 JavaScript 更改 PHP 包含的文件的 CSS

php - 我怎样才能在 codeigniter 中获取系统当前日期和时间

codeigniter - 将 images/css/js 放入 View 文件夹中(codeigniter)

javascript - 如何使用AJAX上传和预览上传的图片

java - 使用 playframework 上传文件时的附加信息

c - 如何多次读取文件 C

php - 使用 git 和 php 进行开发(网络开发)

php - 两个表循环多次的结果集

php - Android应用程序的用户身份验证

php - 从框架到无框架