Daftar dan Belajar Sekarang!

Upload image tidak terganti bila tidak diclear session

foto rdfariz
6 Kontribusi 0 Coto
5 hari yang lalu
gimana cara biar selesai upload gambar, gambar tersebut langsung terganti tanpa harus clear session, ini punyaku mau terganti kalo diclear session dulu

Jawaban
foto irya
54 Kontribusi 11 Coto
5 hari yang lalu
coba code seperti ini.
<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>
update-foto.php
$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{
		........
	}


Login untuk diksusi di forum sekolah koding