【问题标题】:Message: undefined index in CI image upload消息:CI 图像上传中的未定义索引
【发布时间】:2017-07-29 14:30:39
【问题描述】:

我从 1 年开始就在从事 CI 开发人员工作,我没有遇到过这种类型的错误。我尝试通过帖子获取图像名称,但它会给我这样的图像:c:\fakelink\imagename,我不知道是什么问题,请帮助我解决这个问题。

这是我的查看代码:

 <form id="profile_update" enctype="multipart/form-data">

                    <div class="form-group">
                        <img src="<?php echo base_url()?>public/assets/plugins/images/users/varun.png" class="thumb-lg img-circle" alt="img">
                        <div class="row">
                            <div class="form-group col-sm-6">
                                <input type="file" class="form-control" id="user_image" name="user_image">
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <button type="submit" class="btn btn-primary">Submit</button>
                    </div>
                </form>

这里是 Ajax 代码:

 type: "POST",
 url: base_url + "admin/update_profile",
 data: 'user_image=' + user_image,

这是型号代码:

$config = array(
        'upload_path'   => './uploads/',
        'allowed_types' => 'gif|jpg|png|jpeg',
        'max_size'      => '2048',
    );

    if($_FILES['user_image']['name'] != '') {
        $image = 'user_image';
        $upload_data = $this->do_upload($image, $config);
        if ($upload_data['condition'] == 'error') {
            echo json_encode(array('condition' => 'error', 'message' => $upload_data['error'] . ' (Profile image)'));
            exit;
        } else {
            $account_array['foto'] = $upload_data['upload_data']['file_name'];
        }
    }

错误位于这一行:

if($_FILES['user_image']['name'] != '') {

感谢大家的帮助。

【问题讨论】:

    标签: php codeigniter file-upload


    【解决方案1】:

    您的 Ajax 代码不正确。将这些行添加到您的 Ajax:

    var formdata = new FormData ($('#profile_update')[0]); //get form data using HTML5 FormData Object
    
    $.ajax({
       type: "POST",
       url: base_url + "admin/update_profile",
       data: formdata,
       contentType: false, //set content type to false so that the browser will set the content type to multipart
       processData: false,  //Also set process data to false so that the data being sent will not be serialized
       success: function(res){
          //Success function goes here..
       }
    
    });
    

    使用 **name user_image 而不是 id。**

    【讨论】:

      猜你喜欢
      • 2015-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-08
      • 2018-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多