使用CodeIgniter和具有多个输入字段的Ajax在每个输入中上载多个文件

时间:2017-10-10 06:34:40

标签: javascript php jquery ajax codeigniter

我无法在codeigniter中的文件夹中上传多个图片。我已使用此引用Multiple image upload。有人请帮帮我。至少这个参考的作者请帮我解决这个问题。这是我的代码

查看

<input type="file" name="images[]" class="file_input" multiple />
<input type="file" name="images[]" class="file_input" multiple />
<input type="file" name="images[]" class="file_input" multiple />
<input type="file" name="images[]" class="file_input" multiple />

脚本

$(document).ready(function(){
$('#save').on('click', function(){
    var fileInputs = $('.file_input');
    var formData = new FormData();
    $.each(fileInputs, function(i,fileInput){
        if( fileInput.files.length > 0 ){
            $.each(fileInput.files, function(k,file){
                formData.append('images[]', file);
            });
        }
    });
    $.ajax({
        url: '<?php echo base_url(); ?>exerciseset/process', 
                    dataType: 'json', 
                    contentType: false,
                    processData: false,
                    data: formData,
                    method: 'post',

        success: function(response){
            console.log(response);
        }
    });
});
});

控制器

public function process()
{
$fileuploadurl = base_url() . "study_set/";
    $config['upload_path'] = 'study_set/';
    $config['allowed_types'] = 'gif|jpg|png|jpeg|bmp|mp3|mp4';
    $config['max_filename'] = '255';
    $config['encrypt_name'] = FALSE;
    $config['max_size'] = '25000000';
$F = array();

$count_uploaded_files = count( $_FILES['images']['name'] );

$files = $_FILES;
for( $i = 0; $i < $count_uploaded_files; $i++ )
{
    $_FILES['userfile'] = [$files['images']['name'][$i]];

    $F[] = $_FILES['userfile'];

    // Here is where you do your CodeIgniter upload ...
$this->load->library('upload', $config);
                $this->upload->data($_FILES['userfile']); 
                if (!$this->upload->data('images')) {
                    echo $this->upload->display_errors();
                } 
}

echo json_encode($F);
}

它将图像名称返回到数组中,但无法将其上传到文件夹中。而且我还需要在单独的变量中使用每个图像名称。请帮帮我,我很着急。

1 个答案:

答案 0 :(得分:0)

在您的代码中,您似乎错过了上传文件。因此它不会移动到特定文件夹。请在下面查看,希望它能为您提供帮助。

public function process()
{
    $fileuploadurl = base_url() . "study_set/";
    $config['upload_path'] = APPPATH . 'uploads/study_set/';
    $config['allowed_types'] = 'gif|jpg|png|jpeg|bmp|mp3|mp4';
    $config['max_filename'] = '255';
    $config['encrypt_name'] = FALSE;
    $config['max_size'] = '25000000';
    $F = array();

    $count_uploaded_files = count( $_FILES['images']);
    $result = array();
    for( $i = 0; $i < $count_uploaded_files; $i++ )
    {
        $_FILES["file"]["name"] = $_FILES["images"]["name"][$i];
        $_FILES["file"]["type"] = $_FILES["images"]["type"][$i];
        $_FILES["file"]["tmp_name"] = $_FILES["images"]["tmp_name"][$i];
        $_FILES["file"]["error"] = $_FILES["images"]["error"][$i];
        $_FILES["file"]["size"] = $_FILES["images"]["size"][$i];
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('file')) {
            $result['errors'][] = $this->upload->display_errors();
        }else{ 
            $result['success'][] = $this->upload->data(); 
        }
    }
    echo json_encode($result);
}

请确保您指定了正确的文件夹路径,并且文件夹具有足够的权限来写入文件。

如果不起作用,请告诉我。