DOKUMEN PHP

 aksi.php


<?php 

include 'koneksi.php';

$nama = $_POST['nama'];

$kontak = $_POST['kontak'];

$rand = rand();

$ekstensi =  array('pdf','docx','doc','pptx','xlsx');

$filename = $_FILES['file']['name'];

$ukuran = $_FILES['file']['size'];

$ext = pathinfo($filename, PATHINFO_EXTENSION);

if(!in_array($ext,$ekstensi) ) {

header("location:indexi.php?alert=gagal_ekstensi");

}else{

if($ukuran < 154070){

$xx = $rand.'_'.$filename;

move_uploaded_file($_FILES['file']['tmp_name'], 'file2/'.$rand.'_'.$filename);

$simpan = mysqli_query($koneksi, "INSERT INTO dokumen VALUES('','$nama','$kontak','$xx')");

header("location:indexi.php?alert=berhasil");

if ($simpan) {

echo "<script>alert('File Berhasil Di Upload'); 

document.location.href='indexi.php</script>'";

}else{

echo "<script>alert('File Gagal Di Upload'); 

document.location.href='indexi.php</script>'";

}

}else{

header("location:indexi.php?alert=gagal_ukuran");

        }

    }

?>


koneksi.php

<?php 
$koneksi = mysqli_connect('localhost','root','','final');
 
 function hapus ($d) {
  global $koneksi;
  $id = $_GET['id'];
  $sql = mysqli_query($koneksi, "DELETE FROM dokumen WHERE id = '$id'");
  return mysqli_affected_rows($koneksi);
}

function edit($data) {

  global $koneksi;

  $id = $_GET['id'];
  $nama = $_POST['nama'];
  $kontak = $_POST['kontak'];
 
  $rand = rand();
  $ekstensi =  array('docx','pdf','xlsx','pptx');
  $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);
    $sql = "UPDATE dokumen SET nama = '$nama', kontak = '$kontak', file = '$xx' WHERE id = '$id'";
    header("location:indexi.php?alert=berhasil");
    
    if ($sql) {
      echo "<script>alert('File Berhasil Di Upload'); 
          document.location.href='indexi.php</script>'";
    }else{
        echo "<script>alert('File Gagal Di Upload'); 
            document.location.href='indexi.php</script>'";
    }

  }else{
    header("location:indexi.php?alert=gagal_ukuran");
  }
}
  $query = mysqli_query($koneksi, $sql);
  return mysqli_affected_rows($koneksi);
}
 ?>


indexi.php
<?php 
include 'koneksi.php'; 
?>

<!DOCTYPE html>
<html>
<head>
<title>Data Siswa</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="container">
<center>
<h2>Data Data Siswa</h2>
<?php 
if(isset($_GET['alert'])){
if($_GET['alert']=='gagal_ekstensi'){
?>
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4><i></i> Peringatan !</h4>
Ekstensi Tidak Diperbolehkan
<?php
}elseif($_GET['alert']=="gagal_ukuran"){
?>
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4><i></i> Peringatan !</h4>
Ukuran File terlalu Besar
<?php
}elseif($_GET['alert']=="berhasil"){
?>
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4><i></i> Sukses</h4>
File Berhasil Disimpan
<?php
}
}
?>
<br>
<br>
<a href="index.php">Tambah Data</a>
<br>
<br>
<table border="3" cellpadding="7" class="table table-bordered">
<tr>
<th>ID</th>
<th>Nama</th>
<th>Kontak</th>
<th>File</th>
<th> Status </th>
</tr>
<?php 
$data = mysqli_query($koneksi,"SELECT * FROM dokumen");
while($d = mysqli_fetch_array($data)){
?>
<tr>

<td><?php echo $d['id']; ?></td>
<td><?php echo $d['nama']; ?></td>
<td><?php echo $d['kontak']; ?></td>
<td><?php echo $d['file']; ?></td>
<td>

<button><a href="hapus.php?id=<?= $d["id"]; ?>">Hapus</a></button>
<br>
<button><a href="edit.php?id=<?= $d["id"]; ?>">Edit</a></button>

</td>

</tr>
<?php
}
 
?>



</table>
</center>
</body>
</html>

index.php

<!DOCTYPE html>
<html>
<head>
<title>Tambah File</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<style type="text/css">
table{
border-radius: 3%;
border: 3px solid;
border-color: black;
margin: 30px;
}
</style>
<body>
<center>
<table cellpadding="12">
<form action="aksi.php" method="post" enctype="multipart/form-data">
<tr>
<td colspan="2"><center><h2>Tambah Data Siswa</h2></center></td>
</tr>
<tr>
<td><label>Nama</label></td>
<td><input type="text" name="nama" required="required"></td>
</tr>
<tr>
<td><label>Kontak</label></td>
<td><input type="number" name="kontak" required="required"></td>
</tr>
<tr>
<td><label>File</label></td>
<td><input type="file" name="file" required="required"></td>
</tr>
<tr>
<td colspan="2"><p style="color: red">Ekstensi yang diperbolehkan .pdf | .docx | .doc | .pptx</p></td>
</tr>
<br>
<tr>
<td><input type="submit" name="submit" value="Simpan"></td>
<td><button><a href="indexi.php">Kembali</a></button></td>
</tr>
</form>
</table>
</center>
</body>
</html>

hapus.php
<?php 
require 'koneksi.php';
$id = $_GET['id'];
if( hapus($id) > 0 ) {
  echo "
    <script>
      alert('Data Berhasil Dihapus');
      document.location.href = 'indexi.php';
    </script>
  ";

} else {
  echo "
    <script>
      alert('Data Gagal Dihapus')
    </script>

  ";

}
 ?>

edit.php

<?php 

include  'koneksi.php';

$id = $_GET['id'];

$sql = mysqli_query($koneksi, "SELECT * FROM dokumen WHERE id = '$id'");

if (isset($_POST['submit'])) {
if (edit($_POST) > 0) {
echo "
<script>
alert('Gambar Berhasil Diupload');
document.location.href = 'indexi.php';
</script>
";
} else {
echo "
<script>
alert('Gambar Gagal Diupload');
// document.location.href = 'indexi.php';
</script>
";
}
}

 ?>
<br>
<br>

<!DOCTYPE html>

<html>

<head></head>
<style type="text/css">
table{
border-radius: 3%;
border: 3px solid;
border-color: skyblue;
margin-top : 0px;
}
</style>

<body>
<center>
<form method="POST" enctype="multipart/form-data">


<table cellpadding="14">

<?php 
foreach ($sql as $data) {
?>
 
  <input type="number" name="id" value="<?php echo $id; ?>" hidden>
  <tr>
<td colspan="2"><center><h1>Edit Data</h1></center></td>
</tr>
 
  <tr>
  <td><label>Nama</label></td>
<td><input type="text" name="nama" id="nama" value="<?php echo $data["nama"]; ?>"></td>
</tr>
<br>
<br>
<tr>
  <td><label>Kontak</label></td>
<td><input type="number" name="kontak" id="kontak" value="<?php echo $data["kontak"]; ?>"></td>
</tr>

<tr>
<td><label>File</label></td>
<br>
<br>
<td><img src="file/<?php echo $data["file"]; ?>">
<br>
<br>
<input type="file" name="file" required="required" value="<?php echo $data["file"]; ?>"></td>
</tr>
<?php } ?>
  <br>
  <br>
  <tr>
<td colspan="2"><center> <input type="submit" name="submit" value="Edit Data">
<button><a href="indexi.php">Kembali</a></button>
</center></td>
  </tr>
  </table>
</form>
</center>
</body>
</html>

OUTPUT








Komentar

Postingan Populer