1

我制作了一个代码,可以让我上传多个文件但分开

我试图在一个输入中上传多个文件,我将输入标签设置为

<input type="file" multiple="" name="file1">

我选择了 3 张图片,但只上传了 1 张图片。

这是我在更改输入之前的视图:

<?php echo form_open_multipart('test'); ?>
<p>
    <?php echo form_label('File 1: ', 'file1') ?>
    <input type='file' name='file1' id='file1'>
</p>
<p>
    <?php //echo form_label('File 2: ', 'file2') ?>
    <input type='file' name='file2' id='file2'>
</p>
<p><?php echo form_submit('submit', 'Upload them files!') ?></p>

这是我的控制器

function index()
{
    if (isset($_POST['submit']))
    {
        $this->load->library('upload');

        $config['upload_path'] = './upload_documents/';
        $config['allowed_types'] = 'jpg|png|gif|jpeg|JPG|PNG|GIF|JPEG';
        $config['max_size'] = '0'; // 0 = no file size limit
        $config['max_width']  = '0';
        $config['max_height']  = '0';
        $config['overwrite'] = TRUE;

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

        foreach($_FILES as $field => $file)
        {
            // No problems with the file
            if($file['error'] == 0)
            {
                // So lets upload
                if ($this->upload->do_upload($field))
                {
                    $data = $this->upload->data();
                    //alert("nice");
                }
                else
                {
                    $errors = $this->upload->display_errors();
                    die();
                }
            }
            else{
                echo "alksjdfl";
                die();
            }
        }
    }
    $this->load->view("test");
    }
}
4

2 回答 2

1

使用这个多文件上传库

https://github.com/stvnthomas/CodeIgniter-Multi-Upload/blob/master/MY_Upload.php

// Prepraing upload config & upload files
$config = array();
$config['upload_path'] = 'temp/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '256000';
$this->load->library('upload');
$this->upload->initialize($config);
$this->upload->do_multi_upload("my_file");
$files_upload = $this->upload->get_multi_upload_data();
$err_msg = $this->upload->display_errors();

if (!empty($files_upload) && is_array($files_upload)) {

  foreach ($files_upload as $file_data) {

    // Your files here :)
  }
}

特别感谢 @stvnthomas 的图书馆 :)

于 2013-10-03T03:54:07.603 回答
1

您应该使用此库在 CI https://github.com/stvnthomas/CodeIgniter-Multi-Upload中进行多次上传

安装 只需将 MY_Upload.php 文件复制到您的应用程序库目录。

使用:控制器中的函数 test_up

public function test_up(){
    if($this->input->post('submit')){
        $path = './public/test_upload/';
        $this->load->library('upload');
        $this->upload->initialize(array(
            "upload_path"=>$path,
            "allowed_types"=>"*"
        ));
        if($this->upload->do_multi_upload("myfile")){
            echo '<pre>';
            print_r($this->upload->get_multi_upload_data());
            echo '</pre>';
        }
    }else{
        $this->load->view('test/upload_view');
    }
}

应用程序/视图/测试文件夹中的upload_view.php

<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="myfile[]" id="myfile" multiple>
<input type="submit" name="submit" id="submit" value="submit"/>

于 2014-03-26T08:44:15.307 回答