CodeIgniter:多个文件上传多个输入

时间:2014-08-04 04:16:07

标签: file codeigniter input upload

你好,这不是一个问题,而是一个答案。我花了无数个小时试图在Stack Overflow上找到这个问题的简单解决方案,但却无法解决。大多数答案都是关于使用单个输入上传多个文件而人们大多数时候只是建议使用多个上传库来自Git。那么,对于那些真正想要像我这样学习的人来说,这是我最终在网上找到的解决方案。

这就是视图的样子:upload_form.php

<?php echo form_open_multipart('upload');  ?>
<p>
    <?php echo form_label('File 1', 'userfile') ?>
    <?php echo form_upload('userfile') ?>
</p>
<p>
    <?php echo form_label('File 2', 'userfile1') ?>
    <?php echo form_upload('userfile1') ?>
</p>
<p><?php echo form_submit('submit', 'Upload them files!') ?></p>
<?php form_close() ?>

控制器:upload.php

function index()
{
    // Has the form been posted?
    if (isset($_POST['submit']))
    {
        // Load the library - no config specified here
        $this->load->library('upload');

        // Check if there was a file uploaded - there are other ways to
        // check this such as checking the 'error' for the file - if error
        // is 0, you are good to code
        if (!empty($_FILES['userfile']['name']))
        {
            // Specify configuration for File 1
            $config['upload_path'] = 'uploads/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '100';
            $config['max_width']  = '1024';
            $config['max_height']  = '768';       

            // Initialize config for File 1
            $this->upload->initialize($config);

            // Upload file 1
            if ($this->upload->do_upload('userfile'))
            {
                $data = $this->upload->data();
            }
            else
            {
                echo $this->upload->display_errors();
            }

        }

        // Do we have a second file?
        if (!empty($_FILES['userfile1']['name']))
        {
            // Config for File 2 - can be completely different to file 1's config
            // or if you want to stick with config for file 1, do nothing!
            $config['upload_path'] = 'uploads/dir2/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '100';
            $config['max_width']  = '1024';
            $config['max_height']  = '768';

            // Initialize the new config
            $this->upload->initialize($config);

            // Upload the second file
            if ($this->upload->do_upload('userfile1'))
            {
                $data = $this->upload->data();
            }
            else
            {
                echo $this->upload->display_errors();
            }

        }
    }
    else
    {
        $this->load->view("upload_form");
    }
}

瞧!!! 以下是教程的原始链接:http://darrenonthe.net/2011/05/08/upload-multiple-files-with-codeigniter/

1 个答案:

答案 0 :(得分:-1)

您可以使用我找到的代码:

您必须在名为MY_Upload.php的应用程序/库中创建新库 这是图书馆的代码:http://www.beetxt.com/hG2/

版权归任何人所有

控制器功能:

public function gallery()
{
    $this->load->helper(array('form', 'url'));
    $this->load->library('form_validation');
    $this->load->model('admin_model');
    $this->load->library('upload');
    $this->form_validation->set_rules('userfile[]', 'bilde', '');

    if ($this->form_validation->run() == TRUE)
    {

         //Configure upload.
        $this->upload->initialize(array(
            "upload_path"   => './img/gallery/',
            "allowed_types" => 'gif|jpg|png',
            "max_size"  => '9216',
            "max_width"  => '15360',
            "max_height" => '8640',
            "encrypt_name" => TRUE
        ));

    //Perform upload.
        if($this->upload->do_multi_upload("userfile")){
            $imageData = $this->upload->get_multi_upload_data();

            $this->load->library('image_lib');
            foreach($imageData as $item) {
                $config['image_library'] = 'gd2';
                $config['source_image'] = './img/gallery/' . $item['file_name'];
                $config['new_image'] = './img/gallery/thumb/' . $item['file_name'];
                $config['create_thumb'] = TRUE;
                $config['maintain_ratio'] = TRUE;
                $config['width'] = 100;
                $config['height'] = 100;
                $this->image_lib->initialize($config); 
                if( ! $this->image_lib->resize() )
                {
                    die($this->image_lib->display_errors());
                }
                $this->admin_model->insertGallery($item['file_name'], $item['raw_name']."_thumb".$item['file_ext']);
            }

        }
    }

    $data['page'] = "gallery";
    $data['pictures'] = $this->admin_model->getGallery();

    $this->load->view('admin_header', $data);
    $this->load->view('admin_gallery', $data);
    $this->load->view('admin_footer');

}