Kamis, 10 November 2022

Upload file

Index

<?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="tambah.php">Tambah Data</a>

<br>

<br>

<table border="3" cellpadding="7" class="table table-bordered">

<tr>

<th>Nama</th>

<th>Kelas</th>

<th>File</th>

                <th>status</th>

</tr>

<?php 

$data = mysqli_query($koneksi,"SELECT * FROM tbfile");

while($d = mysqli_fetch_array($data)){

?>

<tr>

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

<td><?php echo $d['kelas']; ?></td>

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

<td>

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

<br>


<br>


<button><a href="aksi.php?id=<?= $d["id"]; ?>">Edit</a></button>



</td>

</tr>

<?php

}

 

?>

</table>

</center>

</body>

</html>



Koneksi

<?php 


$koneksi = mysqli_connect('localhost','root','','file');

    if (!$koneksi) {

        echo "Koneksi Gagal";

    

    }

    

    

        function hapus ($data) {

        global $koneksi;

        $id = $_GET['id'];

    

        $sql = mysqli_query($koneksi, "DELETE FROM tbfile WHERE id = '$id'");

    

        return mysqli_affected_rows($koneksi);

    

    }

    

       

 ?>



Tambahan

<!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>Kelas</label></td>

<td><input type="text" name="kelas" 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="index.php">Kembali</a></button></td>

</tr>

</form>

</table>

</center>

</body>

</html>



Aksi

<?php 


include 'koneksi.php';


$nama = $_POST['nama'];

$kelas = $_POST['kelas'];

 

$rand = rand();

$ekstensi =  array('pdf','docx','doc','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);

$simpan = mysqli_query($koneksi, "INSERT INTO tbfile VALUES('','$nama','$kelas','$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");

}

}


?>



Hapus

<?php 


require 'koneksi.php';


$id = $_GET['id'];


if( hapus($id) > 0 ) {


echo " Data Berhasil Di Hapus ";


} else {


echo " Data Tidak Berhasil Di Hapus ";


}


 ?>


 <br>


 <br>


 <button><a href="index.php">Kembali</a></button>

Tidak ada komentar:

Posting Komentar