在 Codeigniter 中上传多个文件

Posted

技术标签:

【中文标题】在 Codeigniter 中上传多个文件【英文标题】:Multiple files upload in Codeigniter 【发布时间】:2013-12-05 12:02:31 【问题描述】:

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

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>

这是我的控制器

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.。有什么问题?

【问题讨论】:

试试这个***.com/questions/1175347/… if ($this->upload->do_upload('images[]')) // 试试这个 您的代码现在可以使用了吗? 我在上面的链接中添加了一个解决方案。参考解决方案:***.com/questions/11524356/… 【参考方案1】:

<?php

if(isset($_FILES[$input_name]) && is_array($_FILES[$input_name]['name']))
            $image_path = array();          
            $count = count($_FILES[$input_name]['name']);   
            for($key =0; $key <$count; $key++)     
                $_FILES['file']['name']     = $_FILES[$input_name]['name'][$key]; 
                $_FILES['file']['type']     = $_FILES[$input_name]['type'][$key]; 
                $_FILES['file']['tmp_name'] = $_FILES[$input_name]['tmp_name'][$key]; 
                $_FILES['file']['error']     = $_FILES[$input_name]['error'][$key]; 
                $_FILES['file']['size']     = $_FILES[$input_name]['size'][$key]; 
                    
                $config['file_name'] = $_FILES[$input_name]['name'][$key];                      
                $this->upload->initialize($config); 
                
                if($this->upload->do_upload('file')) 
                    $data = $this->upload->data();
                    $image_path[$key] = $path ."$data[file_name]";                  
                else
                    $error =  $this->upload->display_errors();
                $this->session->set_flashdata('msg_error',"image upload! ".$error);
                   
            
            return json_encode($image_path);
        
    
    
   ?>

【讨论】:

【参考方案2】:
public function index() 

    $user = $this->session->userdata("username");
    $file_path = "./images/" . $user . '/';

    if (isset($_FILES['multipleUpload'])) 

        if (!is_dir('images/' . $user)) 
            mkdir('./images/' . $user, 0777, TRUE);
        

        $files = $_FILES;
        $cpt = count($_FILES ['multipleUpload'] ['name']);

        for ($i = 0; $i < $cpt; $i ++) 

            $name = time().$files ['multipleUpload'] ['name'] [$i];
            $_FILES ['multipleUpload'] ['name'] = $name;
            $_FILES ['multipleUpload'] ['type'] = $files ['multipleUpload'] ['type'] [$i];
            $_FILES ['multipleUpload'] ['tmp_name'] = $files ['multipleUpload'] ['tmp_name'] [$i];
            $_FILES ['multipleUpload'] ['error'] = $files ['multipleUpload'] ['error'] [$i];
            $_FILES ['multipleUpload'] ['size'] = $files ['multipleUpload'] ['size'] [$i];

            $this->upload->initialize($this->set_upload_options($file_path));
            if(!($this->upload->do_upload('multipleUpload')) || $files ['multipleUpload'] ['error'] [$i] !=0)
            
                print_r($this->upload->display_errors());
            
            else
            
                $this->load->model('uploadModel','um');
                $this->um->insertRecord($user,$name);
            
        
     else 
        $this->load->view('uploadForm');
    


public function set_upload_options($file_path) 
    // upload an image options
    $config = array();
    $config ['upload_path'] = $file_path;
    $config ['allowed_types'] = 'gif|jpg|png';
    return $config;

【讨论】:

以下代码上传多个文件,并为每个用户创建一个目录并上传该目录中的文件,输入元素如下===> 【参考方案3】:
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 true;

【讨论】:

有什么不同?只有返回类型不同?【参考方案4】:

我根据@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;
    

【讨论】:

好的,.fr3 文件是什么文件? pdf是应用程序,html是文本,fr3是? .fr3 = FastReport 模板文件? 这里提到了.fr3? Error: Undefined index: name after the line foreach... 怎么办? $_FILES 是 PHP 内置的 $GLOBALS 之一。你绝对不应该给它分配任何东西。这是非常糟糕的做法。【参考方案5】:

你应该使用这个库在 CI 中进行多重上传 https://github.com/stvnthomas/CodeIgniter-Multi-Upload

【讨论】:

以上是关于在 Codeigniter 中上传多个文件的主要内容,如果未能解决你的问题,请参考以下文章

在 Codeigniter 中上传多个文件并更改名称文件

在codeigniter中上传多个文件,并使用数组重命名每个文件

如何在codeigniter中上传多个/多个图像[重复]

Codeigniter - 上传多个文件,只有一个输入字段

Codeigniter 多文件上传

使用 CodeIgniter 上传多张图片