为什么我在执行此操作时收到 "You did not select a file to upload." 错误消息?

Why do I got "You did not select a file to upload." error message when I did it?

我有一个非常相似的代码,它工作正常,但在这里我收到一条错误消息。我想要一个有输入字段的表格。提交时我想保存文件。但是,当我选择一个文件时,它会给我这个错误信息。为了测试它,我将 required 属性设置为输入字段,这样表单让我提交它,所以我不明白为什么我会收到此消息。你有什么想法吗?

形式:

<form role="form" method="post" id="update-car-form" class="needs-validation" action="<?= base_url(); ?>test/updateCar">
    <input type="hidden" class="form-control" name="carID" id="carID" required>
    <div class="form-row">
        <div class="col-md-4 mb-3">
            <div class="avatar-upload">
                <div class="avatar-edit">
                    <input type='file' name="updateIndex" id="indexImageUpdate" accept=".png" />
                    <label class="text-center" for="indexImageUpdate"></label>
                </div>
                <div class="avatar-preview">
                    <div id="updateIndexImage" style="background-image: url(<?= base_url() ?>img/image_holders/car-front.png)">
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>

<div class="modal-footer">
    <button type="button" class="btn btn-secondary" data-dismiss="modal"><?= $this->lang->line('close') ?></button>
    <button type="submit" class="btn btn-primary" form="update-car-form"><?= $this->lang->line('save') ?></button>
</div>

代码:

if(isset($_FILES['updateIndex']['name']) && !empty($_FILES['updateIndex']['name'])) {
    $plate_number = $this->input->post('updatePlateNumber');
    $config['allowed_types'] = 'jpeg|jpg|png';
    $config['upload_path'] = './img/cars/';
    $config['overwrite'] = TRUE;
    $config['file_name'] = $plate_number;
    $this->load->library('upload', $config);
    if($this->upload->do_upload('updateIndex')) { 
        $this->resize($this->upload->data(), 960, 540);
    }
    else {
        $succesful = false;
        print_r(0);
        print_r($this->upload->display_errors());
        die;
    }   
}

您的表单标记中缺少 enctype="multipart/form-data" 属性。必须存在才能上传图片。

<form enctype="multipart/form-data" role="form" method="post" id="update-car-form" class="needs-validation" action="<?= base_url(); ?>test/updateCar">

根据Official documentation:

加密类型

If the value of the method attribute is post, enctype is the MIME type of the form submission. Possible values:

application/x-www-form-urlencoded: The default value.

multipart/form-data: Use this if the form contains <input> elements with type=file.

text/plain: Introduced by HTML5 for debugging purposes.