Project 8 Upload Gambar PHP dan MySql
koneksi.php
<?php
$koneksi = mysqli_connect("localhost", "root", "", "uploadfile");
?>
tambah.php
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<style type="text/css">
table{
border-radius: 7%;
border: 2px solid;
border-color: black;
margin: 50px;
}
</style>
<body>
<center>
<table cellpadding="15">
<form action="aksi.php" method="POST" enctype="multipart/form-data">
<tr>
<td><label>Nama</label></td>
<td><input type="text" name="nama"></td>
</tr>
<tr>
<td colspan="2"><input type="file" name="file"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit"></td>
</tr>
</form>
</table>
</center>
</body>
</html>
aksi.php
<?php
include 'koneksi.php';
$nama = $_POST['nama'];
$rand = rand();
$ekstensi = array('png','jpg','jpeg','gif');
$filename = $_FILES['file']['name'];
$ukuran = $_FILES['file']['size'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!in_array($ext,$ekstensi) ) {
header("location:index.php?alert=gagal_ekstensi");
}else{
if($ukuran < 1044070){
$xx = $rand.'_'.$filename;
move_uploaded_file($_FILES['file']['tmp_name'], 'file/'.$rand.'_'.$filename);
$simpan = mysqli_query($koneksi, "INSERT INTO project5 VALUES('','$nama','$xx')");
header("location:index.php?alert=berhasil");
if ($simpan) {
echo "<script>alert('File Berhasil Di Upload');
document.location.href='index.php</script>'";
}else{
echo "<script>alert('File Gagal Di Upload');
document.location.href='index.php</script>'";
}
}else{
header("location:index.php?alert=gagal_ukuran");
}
}
?>
index.php
<?php
include 'koneksi.php';
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<center>
<?php
if (isset($_GET['alert'])) {
if ($_GET['alert']=='gagal_ekstensi') {
?>
<h4> Peringatan !</h4>
Ekstensi Tidak Diperbolehkan
<?php
}elseif($_GET['alert']=="gagal_ukuran"){
?>
<h4><i></i> Peringatan !</h4>
Ukuran File terlalu Besar
<?php
}elseif($_GET['alert']=="berhasil"){
?>
<h4><i></i> Sukses</h4>
Berhasil Disimpan
<?php
}
}
?>
<br><br>
<br><br>
<table cellpadding="10" border="2">
<tr>
<th>ID</th>
<th>Nama</th>
<th>File Foto</th>
</tr>
<?php
$data = mysqli_query($koneksi,"SELECT * FROM project5");
while ($d = mysqli_fetch_array($data)) {
?>
<tr>
<td><?php echo $d['id']?></td>
<td><?php echo $d['nama']?></td>
<td><img src="file/<?php echo $d['foto']?>" width="80" height="80"></td>
</tr>
<?php } ?>
<tr>
<td colspan="3"><a href="tambah.php">Tambah Data</a></td>
</tr>
</table>
</center>
</body>
</html>
DatabaseFoto di folder
Komentar
Posting Komentar