0
I’m making a profile screen with photo. The upload and display of it is ok. But when I change some other page data and saved, the image is deleted from the bank (or at least some of the bank). 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);
}
Are you using any framerwork? !
– Wagner Fillio
Hello, thanks for the tip about the tag. The project is in MVC.
– danersphp
If it were me... in your case I wouldn’t save the image url in the database. Would simply upload the image by naming the image as the user ID.
– Wagner Fillio
You are probably not checking whether the image coming from the request is null and is replacing the existing one with a null.
– Danilo Silva
And as I see it?
– danersphp