无法在php

时间:2017-09-09 07:13:19

标签: php codeigniter image-uploading

我正在使用PHP的CodeIgniter框架。我无法上传/更新照片。 这是edit-profile1.php 查看

    <form action="<?php echo base_url();  ?>users/update/" method="post" enctype="multipart/form-data" data-toggle="validator">

    <div class="container">
        <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>

        <div class="container">
        <div class="row">
            <div>
                <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>
        </div>
        </div>
    </form>

这是Users.php 控制器

    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'); 
        //id to be set
        $id = $user->id; 

        if($_FILES['image'])
        {
            /* IMAGE UPLOADING START */
                $image_type=$_FILES['image']['type'];
                $image_size=$_FILES['image']['size'];
                $image_tmp=$_FILES['image']['tmp_name'];
                $d=date('YmdHis');
                //$image_test_name = str_replace(" ", "_", $user->firstname.$user->lastname);
                $image_type = str_replace("image/", ".", $image_type);
                $image_name=$user->id . $image_type;
                $imgurl= str_replace("/index.php/","/",base_url())."media/images/users/".$image_name;   

                if($image_type==".jpeg" || $image_type==".png" || $image_type==".gif" )
                {   
                    $this->usermodel->upload_user_image($image_name);
                    $this->usermodel->update_image($user->id, $image_name);
                    $user->image = $image_name; 
                }
                else
                {
                    $user->image = $this->input->post('oldimage');
                }

            /* IMAGE UPLOADING END */
        }else{
            $user->image = $this->input->post('oldimage');
        }
        $res = $this->db->update('users', $user, array('id' => $id));
        $URL = base_url() . "users/profile/";
        redirect($URL, "refresh"); 
    }

}

这是Users_model.php 模型

public function upload_user_image($image_name) {

 $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();
} else {
  $file_data = $this->upload->data();
  $data['success'] = "Your file is uploaded";
}


}
public function update_image($id,$image_name){

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

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

当页面重定向时,图像会上传。问题是,手动刷新时,占位符图像会再次出现,这意味着照片未更新到数据库。 以下是该网站的链接:https://www.yiyalo.com 您可以使用这些凭证登录 id:qy_leong13@hotmail.com
传球:123456

转到我的帐户&gt;我的个人资料可以试用

1 个答案:

答案 0 :(得分:0)

您需要在控制器本身上传文件。如果您使用的是文件上传库,则无需使用$_FILES变量手动执行此操作。

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'); 
    //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();
    } else {
        $file_data = $this->upload->data();
        $data['success'] = "Your file is uploaded";
    }

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