Codeigniter 3应用程序中此错误图像上传错误消息的原因是什么? [英] What is the cause of this wrong image upload error message in my Codeigniter 3 application?

查看:66
本文介绍了Codeigniter 3应用程序中此错误图像上传错误消息的原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Codeigniter 3.1.8 Bootstrap 4 开发一个基本的博客应用程序。

I am working on a basic blog application with Codeigniter 3.1.8 and Bootstrap 4.

有是编辑帐户信息表格,其中有一个图片上传字段。在控制器中, update()方法包含图像上载操作的逻辑:

There is, among others an "Edit account information" form, which has an image upload field. In the controller, the update() method contains the logic for the image upload action:

public function update() {
    // Only logged in users can edit user profiles
    if (!$this->session->userdata('is_logged_in')) {
        redirect('login');
    }

    $id = $this->input->post('id');

    $data = $this->Static_model->get_static_data();
    $data['pages'] = $this->Pages_model->get_pages();
    $data['categories'] = $this->Categories_model->get_categories();
    $data['author'] = $this->Usermodel->editAuthor($id);

    $this->form_validation->set_rules('first_name', 'First name', 'required');
    $this->form_validation->set_rules('last_name', 'Last name', 'required');
    $this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email');

    $this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');

    // Upload avatar
    $config['upload_path'] = './assets/img/avatars';
    $config['allowed_types'] = 'jpg|jpeg|png';
    $config['max_size'] = '100';

    $this->load->library('upload', $config);

        if (!$this->upload->do_upload('userfile')) {
            $uerrors = array('uerrors' => $this->upload->display_errors());
            $data['uerrors'] = $uerrors;
        }

    if(!$this->form_validation->run() || !empty($uerrors))
    {

        print_r($data['uerrors']);
        $this->load->view('partials/header', $data);
        $this->load->view('dashboard/edit-author');
        $this->load->view('partials/footer');
    } else
    {
        $this->Usermodel->update_user($id);
        $this->session->set_flashdata('user_updated', 'Your account details have been updated');
        redirect(base_url('/dashboard/manage-authors'));
    }
}

我的(令人惊讶的)问题是,即使虽然我正在上传图像,但 print_r($ data ['uerrors']); 返回您没有选择要上传的文件。在浏览器中。

The (surprising) problem I have is that, even though I am uploading an image, print_r($data['uerrors']); returns You did not select a file to upload. in the browser.

在视图中,返回相同的错误:

In the view, the same error is returned:

 <?php echo form_open(base_url('dashboard/users/update')); ?>

        <input type="hidden" name="id" id="uid" value="<?php echo $author->id; ?>">

        <div class="form-group <?php if(form_error('first_name')) echo 'has-error';?>">
          <input type="text" name="first_name" id="first_name" class="form-control" value="<?php echo set_value('first_name', $author->first_name); ?>" placeholder="First name">
          <?php if(form_error('first_name')) echo form_error('first_name'); ?> 
        </div>

        <div class="form-group <?php if(form_error('last_name')) echo 'has-error';?>">
          <input type="text" name="last_name" id="last_name" class="form-control" value="<?php echo set_value('last_name', $author->last_name); ?>" placeholder="Last name">
          <?php if(form_error('last_name')) echo form_error('last_name'); ?> 
        </div>

        <div class="form-group <?php if(form_error('email')) echo 'has-error';?>">
          <input type="text" name="email" id="email" class="form-control" value="<?php echo set_value('email', $author->email); ?>" placeholder="Email">
          <?php if(form_error('email')) echo form_error('email'); ?> 
        </div>

        <div class="form-group <?php if(form_error('bio')) echo 'has-error';?>">
          <textarea name="bio" id="bio" cols="30" rows="5" class="form-control" placeholder="Add a short bio"><?php echo set_value('bio', $author->bio); ?></textarea>
          <?php if(form_error('bio')) echo form_error('bio'); ?> 
        </div>

        <input type="hidden" name="avatar" id="avatar" value="<?php echo $author->avatar; ?>">
          <label for="avatar">Upload avatar</label>
          <div class="form-group">
            <input type="file" name="userfile" id="uavatar" size="20">
            <p><?php print_r($uerrors); ?></p>
          </div>

        <div class="form-group">
          <div class="w-50 pull-left pr-1">
            <input type="submit" value="Update" class="btn btn-block btn-md btn-success">
          </div>
          <div class="w-50 pull-right pl-1">
            <a href="<?php echo base_url('dashboard/users/edit/' . $author->id); ?>" class="btn btn-block btn-md btn-success">Cancel</a>
          </div>
        </div>          
<?php echo form_close(); ?>

我期望的错误消息,考虑到我尝试上传的图像大于指定的图像限制(100KB)为:您尝试上传的文件大于允许的大小

The error message I was expecting, considering that the image I was trying to upload is larger then the specified limit (100KB) is: The file you are attempting to upload is larger than the permitted size.

什么是我做错了吗?

推荐答案

尝试更改表单的开头,从:

Try to change your form opening, from :

<?php echo form_open(base_url('dashboard/users/update')); ?>

<?php echo form_open_multipart(base_url('dashboard/users/update')); ?>

要从 text / plain multipart / form-data 以支持图像数据上传。 此处是每种编码类型之间的区别。

To change the form encoding type from text/plain to multipart/form-data to support image data upload. Here is the difference between each encoding type.

这篇关于Codeigniter 3应用程序中此错误图像上传错误消息的原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆