codeigniter CI中未上传图像

wtlkbnrh  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(123)

我是新的CI,我试图上传图像,但不知道为什么它不上传,这是我的看法,

<form action="<?php echo site_url('admin/test/'.$param2.'/add'); ?>" enctype="multipart/form-data" method="post" id = 'mcq_form'>
    <input type="hidden" name="question_type" value="mcq">
    <div class="input-group">
        <div class="custom-file">
            <input type="file" class="custom-file-input" id="attachment" name="attachment" onchange="changeTitleOfImageUploader(this)">
            <label class="custom-file-label" for="attachment"><?php echo get_phrase('attachment'); ?></label>
        </div>
    </div>
    <div class="text-center">
        <button class = "btn btn-success" id = "submitButton" type="button" name="button" data-dismiss="modal"><?php echo get_phrase('submit'); ?></button>
    </div>
</form>

<script>
    $('#submitButton').click( function(event) {
        $.ajax({
            url: '<?php echo site_url('admin/test/'.$param2.'/add'); ?>',
            type: 'post',
            data: $('form#mcq_form').serialize(),
            success: function(response) {
               if (response == 1) {
                   success_notify('<?php echo get_phrase('question_has_been_added'); ?>');
               }else {
                   error_notify('<?php echo get_phrase('no_options_can_be_blank_and_there_has_to_be_atleast_one_answer'); ?>');
               }
             }
        });
        showLargeModal('<?php echo site_url('modal/popup/test/'.$param2); ?>', '<?php echo get_phrase('test'); ?>');
    });
</script>

在控制器上,我试图获得图像数据,但不知道为什么它不取,

print_r($_FILES['attachment']['name']);
die();

我不明白,我错过了什么。请帮帮我。

rxztt3cl

rxztt3cl1#

你可以这样试试

<input type="file" name="logo" class="form-control" value="">

在控制器中

$logo = '';
if (!empty($_FILES['logo']['name'])) {
    /* Conf Image */
    $file_name = 'profile_' . time() . rand(100, 999);
    $configImg['upload_path'] = './uploads/profile/';
    $configImg['file_name'] = $file_name;
    $configImg['allowed_types'] = 'png|jpg|jpeg';
    $configImg['max_size'] = 2000;
    $configImg['max_width'] = 2000;
    $configImg['max_height'] = 2000;
    $configImg['file_ext_tolower'] = TRUE;
    $configImg['remove_spaces'] = TRUE;

    $this->load->library('upload', $configImg, 'logo');
    if ($this->logo->do_upload('logo')) {
        $uploadData = $this->logo->data();
        $logo = 'uploads/profile/' . $uploadData['file_name'];
    } else {
        $this->custom_errors['logo'] = $this->logo->display_errors('', '');
    }
}

相关问题