Update table without losing information

0

I'm making a photo profile screen. The upload and display is ok. But when I change some other page data and saved, the image is deleted from the database (or at least out of the database). How can I change data on this screen without losing the current profile image?

Screen View:

<?php foreach($info as $info): ?>

<div class="form-group">
        <img src="assets/images/perfil/<?php echo $info['foto']; ?>" border="1" class="perfil_pic" /><br />
        <input type="file" name="foto" class="btn btn-default add_file" style="float:left;"/>
    </div>

<?php endforeach; ?>

Controller da tela:

$u = new Usuarios();

if(isset($_POST['nome']) && !empty($_POST['nome'])){
      $img = $_FILES['foto'];
      $email = addslashes($_POST['email']);
      $senha = base64_encode($_POST['senha']);
      $nome = addslashes($_POST['nome']);
      $sobrenome = addslashes($_POST['sobrenome']);
            $aniversario = addslashes($_POST['aniversario']);
            $bio = addslashes($_POST['bio']);

      $u->updatePerfil($img, $email, $senha, $nome, $sobrenome, $aniversario, $bio);

Screen Model:

public function updatePerfil($pic, $email, $senha, $nome, $sobrenome, $aniversario, $bio){
            $id = $_SESSION['fkr'];
            $url = '';
            if (count($pic) > 0) {
                $tipos = array('image/jpeg','image/jpg','image/png');
                if (in_array($pic['type'], $tipos)) {
                    $url = 'perfilatual';
                    switch($pic['type']){
                        case 'image/jpeg':
                        case 'image/jpg':
                            $url .= '.jpg';
                            break;
                        case 'image/png':
                            $url .= '.jpg';
                            break;
                }
            }
             move_uploaded_file($pic['tmp_name'], 'assets/images/perfil/' . $url);
         }
            $sql = "UPDATE usuarios SET foto = '$url', senha = '$senha', email = '$email', nome = '$nome', sobrenome = '$sobrenome', aniversario = '$aniversario', bio = '$bio' WHERE id = '$id'";
            $this->db->query($sql);
        }
    
asked by anonymous 16.12.2016 / 16:19

1 answer

0

Controller

public function adicionar() {
$data['nome'] = $this->input->post('nome');

if ($this->Usuarios_model->adicionar('usuarios', $data)) {
        $id_usuarios = $this->db->insert_id();

        move_uploaded_file($_FILES['userfile']['tmp_name'], 'upload/imagens_usuarios/' . $id_usuarios . '.jpg');            

        $this->session->set_flashdata('msg','Usuário Adicionado');
        redirect('admin/usuarios/index', 'refresh');
    }

}

public function editar()
{

$data['nome'] = $this->input->post('nome');
if ($this->Usuarios_model->editar('usuarios', $data, 'id', $this->input->post('id')) == TRUE) {

move_uploaded_file($_FILES['userfile']['tmp_name'], 'upload/imagens_usuarios/' . $this->input->post('id') . '.jpg');

$this->session->set_flashdata('msg','Usuário Editado');         
redirect('admin/usuarios/index', 'refresh');

}

}

Place this code on you model:

    //IMAGE URL//
function get_image_url($type = '', $id = '') {
    if (file_exists('upload/imagens_usuarios' . '/' . $id . '.jpg'))
        $image_url = base_url() . 'upload/imagens_usuarios' . '/' . $id .  '.jpg';
    else
        $image_url = base_url() . 'upload/imagens_usuarios/padrao.jpg';

    return $image_url;
}

Use this code to load the image in the View

<input class="file-input" type="file" onchange="preview(this)" name="userfile" style="opacity: 0"/>
<img id="" class="" src="<?php echo $this->Usuarios_model->get_image_url('usuarios');?>" alt="">
    
16.12.2016 / 16:54