I have a problem if the user does not select an image then he can press "Submit" below and then edit it Nothing to do in the database (because there is no image in it), its profile photo is empty and I do not want that I want to show an error that the user has not selected an image.
My second problem is that when the user uploads a new image, how can I remove the old man from the server.
This is my code:
View
& lt ;? Php echo form_open_multipart ('profil / uploadImg'); ? & Gt; & Lt; Div style = "float: none; margin-down: 5px;" Id = "profil_billed" & gt; & Lt; Img width = "200" height = "200" src = "& lt ;? php echo base_url () ;? & gt; images / user / & Lt; Br> & Lt ;? Php echo form_submit ('upload', 'upload bill', 'square = down'); ? & Gt; & Lt ;? Php echo form_hidden ('username', $ this- & gt; session- & gt; user data ('username')); ? & Gt; & Lt ;? Php echo form_close (); ? & Gt; model
function addProfileImg () {$ config = array ('allowed_types' = & gt;' jpg | jpeg | gif | png ',' Upload_path '= & gt; $ this- & gt; gallery_path,' max_size '=> 2000,' encrypt_name '= & gt; true); $ This- & gt; Load-> Library ('upload', $ config); $ This- & gt; Upload-> Do_upload (); $ Image_data = $ this- & gt; Upload-> Data (); $ Config = array ('source_image' = & gt; $ image_data ['full_path'], 'new_image' => $ this-> gallery_path / thumb, 'maintenance' = & gt; true, 'Width' = & gt 200, 'height' => 200, 'encrypt_name' = & gt; true, 'max_size' => 2000); $ This- & gt; Load-> Library ('image_lib', $ config); $ This- & gt; Image_lib- & gt; Resize (); # Rate profile bill shipping # $ $ data = $ this- & gt; Upload-> Data ('file_name'); // print_r ($ data); $ File_array = $ this- & gt; Upload-> Data ('file_name'); $ ProfilBilledNavn ['profile_picture'] = $ file_array ['file_name']; $ This- & gt; Db- & gt; Where ('username', $ this-> input- & gt; post ('username')); $ This- & gt; Db- & gt; Update ('user', $ profilBilledNavn); } Controller
Function Upload Image () {$ this-> form_validation- & gt; Set_rules ('upload', 'upload', 'required'); $ This- & gt; Form_validation- & gt; Set_reales ('userfile', 'userfile', 'required'); If ($ this-> form_validation- & gt; run ()! = True) {$ this-> Load-> Model ('profil_model'); $ This- & gt; Profil_model- & gt; AddProfileImg (); Redirect ('profile / indstillinger /'. $ This-> session-> userdata ('username')); }}
if (! $ This-> upload- & Gt; Do_upload ()) {$ error = array ('error' => $ this-> upload-> display_errors ()); // error .. Show User} Other {$ data = array ('upload_data' => gtc: $ this-> Upload-> Data ()); // save to db} To delete more you have to use unlink () and then you have to delete it from DB. You can use a simple getwhere. Example: $ config ['query'] = $ this- & gt; Db- & gt; Get_where ('picture', 'can be some id here');
Comments
Post a Comment