消息:CI图像上传中的未定义索引

时间:2017-07-29 14:30:39

标签: php codeigniter file-upload

我从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'] != '') {

感谢大家的帮助。

1 个答案:

答案 0 :(得分:0)

您的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..
   }

});
  

使用**名称 user_image代替 id 。**