secrenymou Newbie
Posts: 1
Threads: 1
Joined: Feb 2016
Reputation:
0
I successfully uploaded a user photo in database and ('/uploads' folder) and was able to display but everytime I logged out then when i login again the image/photo disappears.
http://postimg.org/image/fl5yqr8cx/ displayed after upload (userprofile page)
http://postimg.org/image/k2ck7t8hv/ userprofile page when relogin
[url=#][/url]
LOGOUT
PHP Code:
public function logout () { $newdata = array( 'id' => '' , 'memberfname' => '' , 'memberlname' => '' , 'email' => '' , 'image' => '' , 'logged_in' => FALSE , ); $this -> session -> unset_userdata ( $newdata ); $this -> session -> sess_destroy (); $this -> home (); }
CONTROLLER
PHP Code:
public function editphoto ( $id ){ $data = array( "action" => base_url ( '/index.php/Memberlogincontroller/updatephoto/' . $id ), "data" => $this -> db -> get_where ( 'member' ,array( 'id' => $id )) ); $this -> load -> view ( 'member/image' , $data ); } public function upload ( $id ) { if ( (int) $id < 1 ) //$id is not an integer { redirect ( 'memberlogincontroller/member_view' , 'refresh' ); } $this -> load -> helper (array( 'form' , 'file' , 'url' )); $this -> load -> library ( 'form_validation' ); $config_image = array( 'upload_path' => './uploads/' , 'allowed_types' => 'gif|jpg|png' , 'max_size' => '1024' , 'overwrite' => true // 'max_width' => '1024', // 'max_height' => '768', ); $this -> load -> library ( 'upload' , $config_image ); if ( $this -> form_validation -> run ()== false and empty( $_FILES [ 'userfile' ][ 'name' ][ 0 ])) { $error = array( 'error_image' => 'Please choose image to upload' , ); $this -> load -> view ( 'member/image' , $error ); }elseif( $this -> form_validation -> run ()== true and empty( $_FILES [ 'userfile' ][ 'name' ][ 0 ])){ $error = array( 'error_image' => 'Please choose image to upload' , ); $this -> load -> view ( 'member/image' , $error ); }elseif( $this -> form_validation -> run ()== false and !empty( $_FILES [ 'userfile' ][ 'name' ][ 0 ])){ ! $this -> upload -> data (); $error = array( 'error_image' => '' , ); $this -> load -> view ( 'member/image' , $error ); }elseif( $this -> form_validation -> run ()== true and !empty( $_FILES [ 'userfile' ][ 'name' ][ 0 ])){ $this -> upload -> do_upload (); $data = array( 'upload_data' => $this -> upload -> data ()); $this -> image -> resize ( $data [ 'upload_data' ][ 'full_path' ], $data [ 'upload_data' ][ 'file_name' ]); $profile = array( 'image' => $data [ 'upload_data' ][ 'file_name' ] ); $this -> db -> update ( 'member' , $profile ,array( 'id' => $id )); $this -> session -> set_userdata ( $profile ); redirect ( 'index.php/memberlogincontroller/getMember/' . $id , 'refresh' ); } } public function image_resize ( $path , $file ){ $config_resize =array( 'image_library' => 'gd2' , 'source_image' => $path , 'maintain_ratio' => TRUE , 'width' => 75 , 'height' => 50 , 'new_image' => './uploads/thumb/' . $file ); $this -> load -> library ( 'image_lib' , $config_resize ); $this -> image_lib -> resize (); } public function updatephoto ( $id ){ if ((int) $id < 1 ) //$id is not an integer { redirect ( 'memberlogincontroller/member_view' , 'refresh' ); } else{ $this -> load -> helper (array( 'form' , 'file' , 'url' )); $this -> load -> library ( 'form_validation' ); $config_image = array( 'upload_path' => './uploads/' , 'allowed_types' => 'gif|jpg|png' , 'max_size' => '1024' , 'overwrite' => true // 'max_width' => '1024', // 'max_height' => '768', ); $this -> load -> library ( 'upload' , $config_image ); if ( $this -> form_validation -> run ()== false and empty( $_FILES [ 'userfile' ][ 'name' ][ 0 ])) { $id = $this -> session -> userdata ( 'id' ); $memberinfo = array( 'error_image' => '' , ); $this -> load -> model ( 'MemberloginModel' ); $memberinfo [ 'memberinfo' ] = $this -> Memberloginmodel -> getMember ( $id ); $this -> load -> view ( 'member/image' , $memberinfo ); } else { $this -> upload -> do_upload (); $data = array( 'upload_data' => $this -> upload -> data ()); $this -> image_resize ( $data [ 'upload_data' ][ 'full_path' ], $data [ 'upload_data' ][ 'file_name' ]); $id = $this -> session -> userdata ( 'id' ); $this -> db -> where ( 'id' , $id ); $query = $this -> db -> get ( 'member' ); /* foreach($query->result() as $row) { unlink('./uploads/'.$row->image); unlink('./uploads/thumb/'.$row->image); } */ $data = array( 'image' => $data [ 'upload_data' ][ 'file_name' ] ); $this -> db -> update ( 'member' , $data ,array( 'id' => $id )); $this -> session -> set_userdata ( $data ); redirect ( 'index.php/memberlogincontroller/getMember/' . $id , 'refresh' ); } } }
Model
PHP Code:
class Memberloginmodel extends CI_Model { public function __construct () { parent :: __construct (); $this -> load -> helper (array( 'url' , 'html' , 'form' )); } function login () { $this -> db -> where ( 'email' , $this -> input -> post ( 'email' )); $this -> db -> where ( 'password' , md5 ( $this -> input -> post ( 'password' ))); $query = $this -> db -> get ( 'member' ); /*i added 'member' table on db new members*/ if( $query -> num_rows ()> 0 ) { foreach ( $query -> result () as $rows ) { //add all data to session $newdata = array( 'id' => $rows -> id , 'memberfname' => $rows -> memberfname , 'memberlname' => $rows -> memberlname , 'email' => $rows -> email , 'logged_in' => TRUE ); } $this -> session -> set_userdata ( $newdata ); return true ; }else{ return false ; } } public function add_user () { $data = array( 'memberfname' => $this -> input -> post ( 'memberfname' ), 'memberlname' => $this -> input -> post ( 'memberlname' ), 'email' => $this -> input -> post ( 'email' ), 'password' => md5 ( $this -> input -> post ( 'password' )), ); $this -> db -> insert ( 'member' , $data ); } public function getMember ( $id ) { $this -> db -> where ( 'id' , $id ); $query = $this -> db -> get ( 'member' ); if ( $query -> num_rows () !== 1 ) { return FALSE ; } return $query -> row (); }
upload image view
PHP Code:
< form action = "<?php echo $action ;?>" method = "POST" enctype = "multipart/form-data" > // echo $action, refer to memberlogincontroller/editphoto < a href = "#" >< img height = "200px" width = "200px" src = "<?php echo base_url().'/uploads/'. $this -> session ->userdata('image'); ?>" > < input type = "file" name = "userfile" /> < br />< br /> < input type = "hidden" name = "id" value = "<?php echo $this -> session ->userdata('id'); ?>" /> < input type = "submit" name = "submit" id = "submit" class= "btn btn-primary" value = "upload" /> </ form >
userprofilepage
PHP Code:
< div class= "description" > <!-- < a href = "#" >< img height = "200px" width = "200px" src = "<php echo base_url();?>uploads/no-avatar.jpg" ></ a >--> < a href = "#" >< img height = "200px" width = "200px" src = "<?php echo base_url().'/uploads/'. $this -> session ->userdata('image'); ?>" > </ div >
Attached Files
Thumbnail(s)
skunkbad Senior Citizen
Posts: 1,300
Threads: 63
Joined: Oct 2014
Reputation:
86
When somebody logs in, in your model, you are not setting the image.
Code:
$newdata = array(
'id' => $rows->id,
'memberfname' => $rows->memberfname,
'memberlname' => $rows->memberlname,
'email' => $rows->email,
'logged_in' => TRUE
);
If you add the image to the $newdata array, it should work, no?