Postingan lainnya
Upload image tidak terganti bila tidak diclear session
gimana cara biar selesai upload gambar, gambar tersebut langsung terganti tanpa harus clear session, ini punyaku mau terganti kalo diclear session dulu
1 Jawaban:
coba code seperti ini. <pre> <form action="update-foto.php" method="POST" enctype="multipart/form-data"> <div class="form-group"> <input type="hidden" name="username" value="<?php echo $_SESSION['username'] ?>"> </div> <label>Upload</label> <input type="file" name="foto"> <div class="form-group" style="margin-top: 10px;"> <input type="submit" class="btn btn-info" style="margin-right: 10px;" value="Ganti" required> <input type="reset" class="btn btn-danger" value="Reset"> </div> </form> </pre>
update-foto.php <pre> $username = $_POST['username']; $foto = $_FILES['foto']['name']; $type = array('image/jpg', 'image/jpeg', 'image/png'); $target = "img/".basename($_FILES['foto']['name']);
if (in_array($_FILES['foto']['type'], $type)) {
$sql = mysqli_query($conn,"SELECT * FROM admin WHERE username='$username'");
$data = mysqli_fetch_array($sql);
if (file_exists($target.$data['foto'])) {
unlink($target.$data['foto']);
move_uploaded_file($_FILES['foto']['tmp_name'], $target);
$sql2 = mysqli_query($conn,"UPDATE admin SET foto='$foto' WHERE username='$username'");
header("location:ganti_foto.php");
}else{
move_uploaded_file($_FILES['foto']['tmp_name'], $target);
$sql2 = mysqli_query($conn,"UPDATE admin SET foto='$foto' WHERE username='$username'");
header("location:ganti_foto.php");
}
}else{
........
}
</pre>