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

avatar rdfariz
@rdfariz

7 Kontribusi 0 Poin

Diperbarui 5 tahun yang lalu

1 Jawaban:

coba code seperti ini. <pre> &lt;form action="update-foto.php" method="POST" enctype="multipart/form-data"&gt; &lt;div class="form-group"&gt; &lt;input type="hidden" name="username" value="&lt;?php echo $_SESSION['username'] ?&gt;"&gt; &lt;/div&gt; &lt;label&gt;Upload&lt;/label&gt; &lt;input type="file" name="foto"&gt; &lt;div class="form-group" style="margin-top: 10px;"&gt; &lt;input type="submit" class="btn btn-info" style="margin-right: 10px;" value="Ganti" required&gt; &lt;input type="reset" class="btn btn-danger" value="Reset"&gt; &lt;/div&gt; &lt;/form&gt; </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[&#039;foto&#039;][&#039;type&#039;], $type)) {
	$sql = mysqli_query($conn,&quot;SELECT * FROM admin WHERE username=&#039;$username&#039;&quot;);
	$data = mysqli_fetch_array($sql);
	if (file_exists($target.$data[&#039;foto&#039;])) {
		unlink($target.$data[&#039;foto&#039;]);
		move_uploaded_file($_FILES[&#039;foto&#039;][&#039;tmp_name&#039;], $target);
		$sql2 = mysqli_query($conn,&quot;UPDATE admin SET foto=&#039;$foto&#039; WHERE username=&#039;$username&#039;&quot;);
		header(&quot;location:ganti_foto.php&quot;);
	}else{
		move_uploaded_file($_FILES[&#039;foto&#039;][&#039;tmp_name&#039;], $target);
		$sql2 = mysqli_query($conn,&quot;UPDATE admin SET foto=&#039;$foto&#039; WHERE username=&#039;$username&#039;&quot;);
		header(&quot;location:ganti_foto.php&quot;);
	}
}else{
	........
}

</pre>

avatar irya
@irya

59 Kontribusi 13 Poin

Dipost 5 tahun yang lalu

Login untuk ikut Jawaban