03-21-2013, 09:27 PM
[eluser]haris244808[/eluser]
Hi there...
i am trying to update an image upload together with some other info...
If other fileds(ex: name, surname) are posted and the image is not posted...i just want to update(insert) those fields only to database...
if the image is uploaded...i want to delete the old image from the folder together with the image name from the database and update the new one
here is what i have done so far: (i made some explanations through php comments also)
it works well....but i want that when i dont choose an image...not to retrieve the old image name from another query...is there any other solution...
A solution that dont require a query to retrieve the old name...any other solution that will not overlap the performance...
Hi there...
i am trying to update an image upload together with some other info...
If other fileds(ex: name, surname) are posted and the image is not posted...i just want to update(insert) those fields only to database...
if the image is uploaded...i want to delete the old image from the folder together with the image name from the database and update the new one
here is what i have done so far: (i made some explanations through php comments also)
it works well....but i want that when i dont choose an image...not to retrieve the old image name from another query...is there any other solution...
A solution that dont require a query to retrieve the old name...any other solution that will not overlap the performance...
Code:
$picture = 'profile_pic';
$users = $this->users_model->select_user_by_id($user_id);
foreach ($users as $results) {
$pic_name = $results->profile_pic; //here i get the old pictures name
}
if($this->form_validation->run() == TRUE){
if($_FILES[$picture]['name'] && $_FILES[$picture]['size'] > 0 ){ //here i check if the Image is uploaded or not
$picture_path = FCPATH.'uploads/profile_pictures/original/'.$pic_name; //retrieve old image path
$thumb_path = FCPATH.'uploads/profile_pictures/171x214/'.$pic_name;
unlink($picture_path); //delete old image from the path
unlink($thumb_path);
$config['upload_path'] = FCPATH.'uploads/profile_pictures/original/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
//when you change this, the upload_max_filesize in php.ini file must be changed too
$config['max_size'] = '2000';
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
if($this->upload->do_upload($picture)){ //if upload of picture is successfull
$data = $this->upload->data(); //get all the uploads file data(name, path...)
$image = $data['full_path'];
$this->load->model('images_model'); //load images_model
if ($this->images_model->image_resize($image) == TRUE) { //resize image
$profile_pic_name = $data['file_name']; //get the name of the picture
}
}
}
$pic = $profile_pic_name ? $profile_pic_name : $pic_name; //if $profile_pic_name (which is new name) is set assign that to $pic otherwise assign the old picture name to $pic
$user_query['user_data'] = $this->users_model->update_user($user_id, $pic);//pass picture name to update in database