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);
}