【问题标题】:How can I upload multiple images on single file upload using Codeigniter?如何使用 Codeigniter 在单个文件上传时上传多个图像?
【发布时间】:2014-09-16 01:57:46
【问题描述】:

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

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

控制器/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');
    }

views/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(); ?>

views/v_document/upload_error.php

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

views/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'); ?>

我的程序运行良好。我可以一张一张上传图片,但我想要的是一键上传多张图片。我需要它,因为我们需要在我们的网站上上传大量文档。

希望得到大家的帮助。

【问题讨论】:

  • 我正在学习node.js,我读到他们可以上传多个文件,所以试着学习一下。

标签: php codeigniter twitter-bootstrap file-upload


【解决方案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());
            .
            .
            .

          }
    }

干杯。

【讨论】:

  • 我用过一次……有什么问题吗?
  • 我认为您需要将 $index 作为参数传递给 do_upload 方法,而不是 $file
  • 嗯...可能是这种情况...@xxxxxxz 请您验证并提供反馈
  • 我收到此错误消息:isset 中的偏移类型非法或为空
  • 结果只有最后一张图片在上传,变成3。如果我选​​择mouse.jpg,keyboard.jpg,monitor.jpg。输出:monitor1.jpg、monitor2.jpg、monitor3.jpg
【解决方案2】:

你可以使用这个库...似乎比其他选择更快。

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

【讨论】:

  • 现在我们正在谈论@Dirgh..这似乎是避免头痛的完美解决方案......
  • 我试图这样做。同样的事情也会发生。我认为因为该库不适合单击并能够运行它,所以我需要创建另一个文件按钮。这是我有 10 个文件上传按钮的结果 T_T
  • 我不这么认为......因为当我检查文件框的多个属性时它工作正常
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-08
  • 2012-12-06
  • 2015-01-07
  • 1970-01-01
  • 2015-10-04
  • 2018-08-01
相关资源
最近更新 更多