Cara Membuat Upload PDF Sederhana dengan PHP 7 dan MYSQLi di Webserver Apache


Assalamualaikum Wr.Wb...
dengan diberikan nikmat dan rezeki dari Allah SWT, saya Mr.Gagaltotal666 akan
berbagi kepada anda yaitu tentang Cara Membuat Upload PDF Sederhana
dengan PHP 7 dan MYSQLi di Webserver Apache..

upload file dengan format PDF ini sangat lah banyak yang dicari
karena untuk mendaftar suatu persyaratan membutuhkan suatu file
dengan format PDF ataupun bisa juga image, oke langsung saja

sebelum menuju ke tutorial, anda siapkan berupa package PHP 7
disini saya menggunakan PHP 7.4 di Linux server saya
dan webserver yang saya gunakan adalah apache
tentu dengan sesuai judul PHP 7 menggunakan sintak query Mysqli
berbeda dengan PHP 5, ada PHP 5 dengan versi 5.30 yang bisa Mysqli

oke buka text editor kesayangan anda sendiri...

pertama seperti biasa membuat koneksi untuk ke database Mysql
contoh sederhana koneksi berikut, di file koneksi.php

<?php
$server = "127.0.0.1";
$username = "username";
$password = "password";
$database = "upload_pdf";
$koneksi = new mysqli($server,$username,$password,$database);
?>

selanjut nya buat tampilan depan dan form untuk mengupload file PDF
contoh index.php ...

<?php
include "koneksi.php";
?>
<!DOCTYPE html>
<html>
<head>
 <title>Mengupload File PDF Dengan PHP</title>
 <style type="text/css">
 body {
  font-family: verdana;
  font-size: 12px;
 }
 a {
  text-decoration: none;
  color: #3050F3;
 }
 a:hover {
  color: #000F5E;
 }
</style>
</head>
<body>
<h1>Mengupload File PDF Dengan PHP</h1>
<hr>
<form action="upload.php" method="POST" enctype="multipart/form-data">
<table width="600" border="0">
<tr>
 <td width="100">Judul File</td>
 <td><input type="text" name="judul" placeholder="Judul" required></td>
</tr>
<tr>
 <td width="100">File PDF</td>
 <td><input type="file" name="nama_file" required></td>
</tr>
<tr>
 <td width="100"></td>
 <td><input type="submit" value="Upload File"></td>
</tr>
</table>
</form>
<hr>
<b>List File PDF</b>
<table width="800" border='0' cellpadding="2" cellspacing="1" bgcolor="#000000">
<tr>
 <th bgcolor="#ffffff">ID</th>
 <th bgcolor="#ffffff">Judul</th>
 <th bgcolor="#ffffff" width="100">View</th>
 <th bgcolor="#ffffff" width="100">Hapus</th>
</tr>
<?php
$query = mysqli_query($koneksi,"SELECT * FROM data_file ORDER BY id ASC");
while($data = mysqli_fetch_array($query)){
?>
<tr>
 <td bgcolor="#ffffff"><?php echo $data['id'];?></td>
 <td bgcolor="#ffffff"><?php echo $data['judul'];?></td>
 <th bgcolor="#ffffff"><a href="view.php?id=<?php echo $data['id'];?>">Lihat File</a></th>
 <th bgcolor="#ffffff"><a href="hapus.php?id=<?php echo $data['id'];?>">Hapus File</a></th>
</tr>
<?php
}
?>
</table>
</body>
</html>


Query sintak Mysqli ini contoh

<?php
$query = mysqli_query($koneksi,"SELECT * FROM data_file ORDER BY id ASC");
while($data = mysqli_fetch_array($query)){
?>

untuk menampilkan seluruh data di table data_file dengan urutan Ascending

oke lanjut, buat file aksi untuk upload, contoh di form dalam action
saya mengasih file yaitu upload.php
yang nanti nya akan merequest ke file tersebut, buat file upload.php

<?php
include "koneksi.php";
//pengecekan tipe harus pdf
$tipe_file = $_FILES['nama_file']['type']; //mendapatkan mime type
if ($tipe_file == "application/pdf"){ //mengecek apakah file tersebu pdf atau bukan
 $judul     = trim($_POST['judul']);
 $nama_file = trim($_FILES['nama_file']['name']);
 $sql = "INSERT INTO data_file (judul) VALUES ('$judul')";
 mysqli_query($koneksi,$sql); //simpan data judul dahulu untuk mendapatkan id
 //dapatkan id terkahir
 $query = mysqli_query($koneksi,"SELECT id FROM data_file ORDER BY id DESC LIMIT 1");
 $data  = mysqli_fetch_array($query);
 //mengganti nama pdf
 $nama_baru = "file_".$data['id'].".pdf"; //hasil contoh: file_1.pdf
 $file_temp = $_FILES['nama_file']['tmp_name']; //data temp yang di upload
 $folder    = "file"; //folder tujuan
 move_uploaded_file($file_temp, "$folder/$nama_baru"); //fungsi upload
 //update nama file di database
 mysqli_query($koneksi,"UPDATE data_file SET nama_file='$nama_baru' WHERE id='$data[id]'");
 header('location:index.php?pesan=upload-berhasil');
}else{
 echo'<div class="alert alert-danger">File gagal di upload senpai T_T.</div>';
}
?>


kemudian buat file view.php untuk melihat detail yang barusan di upload
contoh isi dari file view.php

<?php
include "koneksi.php";
?>
<!DOCTYPE html>
<html>
<head>
 <title>Mengupload File PDF Dengan PHP</title>
 <style type="text/css">
 body {
  font-family: verdana;
  font-size: 12px;
 }
 a {
  text-decoration: none;
  color: #3050F3;
 }
 a:hover {
  color: #000F5E;
 }
</style>
</head>
<body>
<?php
$id    = mysqli_real_escape_string($koneksi,$_GET['id']);
$query = mysqli_query($koneksi,"SELECT * FROM data_file WHERE id='$id' ");
$data  = mysqli_fetch_array($query);
?>
<h1>Mengupload File PDF Dengan PHP</h1>
<hr>
<b>Judul:</b> <?php echo $data['judul'];?> | <a href='index.php'> Kembali </a>
<hr>
<embed src="file/<?php echo $data['nama_file'];?>" type="application/pdf" width="800" height="600" ></embed>
</body>
</html>


setelah itu buat aksi delete, buat file hapus.php
contoh sebagai berikut

<?php
//HAPUS
include "koneksi.php";
$id = mysqli_real_escape_string($koneksi,$_GET['id']);
$sql = mysqli_query($koneksi,"SELECT * from data_file where id='$id'");
$row = mysqli_fetch_array($sql);
unlink("file/$row[nama_file]");
$result = mysqli_query($koneksi, "DELETE FROM data_file WHERE id ='$id'");
if ($result){ ?>
<script language="javascript">
alert('Data Berhasil Dihapus senpai ^_<');
document.location.href="index.php";
</script>
<?php
}else{
  ?>
  <script language="javascript">
alert('Yah Data ga Berhasil Dihapus senpai T_T');
document.location.href="index.php";
</script>
  <?php
}
?>


query sintak diatas sudah mencegah dari SQL Injection dan untuk menghapus
file nya dapat automatis dari folder akan terhapus jadi tidak menyisa di folder

gambaran contoh...



untuk file source code nya bisa kamu download dibawah ini
ingat kembangkan lagi yah ^_^ selamat mengerjakan...

Upload PDF di PHP 7

link baru jika link di atas mati, klik [disini ya]

PASSWORD RAR : [gagaltotal666]
ingat password rar huruf kecil semua

oke mungkin itu saja mengenai upload PDF di PHP 7 native
kalau ada kesalahan dan kekurangan nya mohon berikan masukan nya ya

sekian dan semoga bermanfaat...

Wasalamualaikum Wr.Wb...

Post a Comment

2 Comments

  1. Replies
    1. tidak bisa di buka kenapa ?, itu link di atas sudah saya perbaiki ya, dan password rar nya juga sesuai, coba kemungkinan ISP kamu dan file di download corup, bisa download kembali

      Delete
Emoji
(y)
:)
:(
hihi
:-)
:D
=D
:-d
;(
;-(
@-)
:P
:o
:>)
(o)
:p
(p)
:-s
(m)
8-)
:-t
:-b
b-(
:-#
=p~
x-)
(k)