如何使用Codeigniter在单个文件上传时上传多个图像?

时间:2014-09-16 01:57:46

标签: php codeigniter twitter-bootstrap file-upload

我正在创建一个程序,我希望上传一个文件来选择多个图像并将其保存到数据库中。我不是那么天才的编码,但我理解编程。

这是我到目前为止所做的,

控制器/ upload.php的

<?php

class Upload extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
        $this->load->helper('date');
    }

    public function index() {
        $this->load->view('v_page/header_view');
        $this->load->view('v_document/upload_view', array('error' => ' ' ));
        $this->load->view('v_page/footer_view');
    }

    public function do_upload() {
        //print_r($_FILES);
        $config = array(
            'upload_path'   => './public/img/uploads',
            'upload_url'    => base_url().'public/img/uploads',
            'allowed_types' => 'gif|jpg|png|jpeg',
            //'overwrite'     => TRUE,
            'max_size'      => '1000KB',
            'max_width'     => '1024',
            'max_height'    => '768',
            //'encrypt_name'  => true,
        );

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

        if (!$this->upload->do_upload()) {
            $error = array('error' => $this->upload->display_errors());
            $this->load->view('v_page/header_view');
            $this->load->view('v_document/upload_error', $error);
            $this->load->view('v_document/upload_view');
            $this->load->view('v_page/footer_view');
        } 
        else {
            $upload_data = $this->upload->data();
            $file_array = array(
                'image_name'    => $upload_data['file_name'],
                //'description'   => "",
                'date_created'  => date('Y-m-d H:i:s', now()),
                'date_modified' => date('Y-m-d H:i:s', now()),
                'size'          => $upload_data['file_size'],
                'type'          => $upload_data['image_type'],
                'width'         => $upload_data['image_width'],
                'height'        => $upload_data['image_height'],
                'actions'       => "modify",
            );
            $this->load->database();
            $this->db->insert('tbl_image', $file_array);
            $data = array('upload_data' => $this->upload->data());
            $this->load->view('v_document/upload_success', $data);
        }
    }

    public function upload_success() {
        redirect('do_upload', 'refresh');
    }

视图/ v_document / upload_view.php

<p class="alert alert-info">Upload New Document</p>
<?php echo form_open_multipart('upload/do_upload');?>
<label for="file">Select File To Upload:</label>
<input type="file" name="userfile" multiple="multiple" class="btn btn-file"/>
</br></br>
<input type="submit" value="Upload File" class="btn btn-primary"/>
<?php echo form_close(); ?>

视图/ v_document / upload_error.php

<div class="alert alert-error"><?php echo $error; ?></div>

视图/ v_document / upload_success.php

<?php $this->load->view('v_page/header_view'); ?>

    <div class="alert alert-success">Your file was successfully uploaded!</div>
    <ul>
        <?php foreach ($upload_data as $item => $value):?>
        <li><?php echo $item;?>: <?php echo $value;?></li>
        <?php endforeach; ?>
    </ul>
    <p>
        <?php echo anchor(base_url().'public/img/uploads/' . $upload_data['file_name'], 'View uploaded image') ?>
        </br>
        <?php echo anchor('upload', 'Upload another image'); ?>
    </p>

<?php $this->load->view('v_page/footer_view'); ?>

我的计划运作良好。我可以逐个上传图片,但我想要的是一键上传多张图片。我需要这个,因为我们需要在我们的网站上上传大量文档。

希望得到你们的帮助。

2 个答案:

答案 0 :(得分:1)

您需要做的是将文件标记的名称更改为数组,如

<input type="file" name="userfile[]" multiple="multiple" class="btn btn-file"/>

然后你需要把上传代码放到foreach中,如下所示

 foreach($_FILES['userfile'] as $file)
   {
       if (!$this->upload->do_upload($file)) {
            $error = array('error' => $this->upload->display_errors());
            .
            .
            .

          }
    }

干杯。

答案 1 :(得分:1)

您可以使用此库...似乎比其他选择更快。

https://github.com/stvnthomas/CodeIgniter-Multi-Upload

添加您需要做的事后

控制器更改:

//upload multiple file 

 if($this->upload->do_multi_upload("files"){
            //Code to run upon successful upload.
        }    

//get data of uploaded files

  print_r($this->upload->get_multi_upload_data());

我希望您使用的是codeigniter 2.x