图像上传不能使用PHP工作

时间:2017-09-21 13:56:42

标签: php codeigniter upload

以下是editprofile1 视图

<form action="<?php echo base_url(); ?>users/update/" method="post" enctype="multipart/form-data" data-toggle="validator">
    <div class="padd col-xs-12 col-md-3">
        <label class="label">My IC/Matric Card</label></br>
        <input name="image"  placeholder="Upload" class="upload friendly"  type="file" accept="image/*" capture="environment" required>
    </div>
    <div class="col-xs-12 col-md-9 padd" style="overflow:auto;">
        <div class="picture"><img id="blah" style="width: 100%; height: 100%"  
            <?php
            if ($userdata[0]['image'] == "") {
                echo "src='https://style.anu.edu.au/_anu/4/images/placeholders/person.png'";
            } else {
                echo "src='" . str_replace('/index.php/', '/', base_url()) . "media/images/users/" . $userdata[0]['image'] . "'";
            }
            ?> >
        </div>
    </div>
    <div class="row padd">          
        <button type="submit" style='width: 100%; margin-top:20px; margin-bottom: 20px !important; background-color: #59d389; color: white;' class="btn btn-responsive  btn-md center-block">Save Changes</button>
        <input type="hidden"   value="<?php echo $userdata[0]['id']; ?>"  name="id" />
        <input type="hidden"   value="<?php echo $userdata[0]['account_status']; ?>"  name="id" />
                    <!-- <input type="hidden"   value="<?php echo $userdata[0]['image']; ?>"  name="oldimage"/>  -->
    </div>
</form>

以下是用户控制器

public function profile()
    {
        $data['userdata'] = $this->usermodel->get_user($this->session->userdata('id'));
        $this->load->view('edit-profile1',$data);
    }

    public function update()
    {
        $user = new Users_model(); 
        // $user->phone1 = $this->input->post('phone1');
        $user->account_status = 1;
        //session data
        $user->id = $this->session->userdata('id');
        $user->password = $this->session->userdata('password');
        $user->username = $this->session->userdata('username');
        $user->email = $this->session->userdata('email');
        $user->user_role = $this->session->userdata('user_role'); 
        $image_type=$_FILES['image']['type'];
        $image_type = str_replace("image/", ".", $image_type);
        $image_name=$user->id . $image_type;
        //id to be set
        $id = $user->id; 
        $config['upload_path'] = 'media/images/users';
        $config['allowed_types'] = 'jpg|jpeg|png|gif';
        $config['overwrite'] = TRUE;
        $config['file_name'] = $image_name;

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

        if (!$this->upload->do_upload('image')) {
            $this->upload->display_errors('<span>', '</span>');
            $data['error'] = $this->upload->display_errors();
            // $user->image = $this->input->post('oldimage');
        } else {
            $file_data = $this->upload->data();
            $data['success'] = "Your file is uploaded";
            $this->usermodel->update_image($user->id, $image_name);
            $user->image = $image_name; 
        }

        $res = $this->db->update('users', $user, array('id' => $id));
        // $ans =$this->db->last_query();
        //  echo $ans;
         $URL = base_url() . "users/profile/";
         redirect($URL, "refresh"); 
    }

以下是Users_model.php 模型

public function update_image($id,$image_name){

  $sql = "update users set image = '$image_name' where id = $id ";

  $this->db->query($sql);

}

上面的代码应该将图像上传到数据库,并将图像名称修改为与具有图像类型扩展名的用户ID相同,并且可以重写。但是,图像未上传到数据库。重新加载配置文件页面后,不会显示任何图像。

您可以登录

https://www.yiyalo.com尝试一下。
ID:qy_leong13@hotmail.com
通过:123456

0 个答案:

没有答案