Cara Membuat Upload File / Berkas Dengan Php 7




Tampilan Table : 


Tampilan Form : 


Kemudian Buat lah dua buah table di dalam database yaitu : 
1. Buat lah database dengan nama "upload"
1. tbl_jenis_file
2. tbl_file
3. buat lah folder "file" yang mana nanti untuk menyimpan file yg di upload 

1. struktur table tbl_jenis_file : 


isi lah tabel tbl_jenis_file dengan data berikut : 


2. struktur table tbl_file 


Code : Form HTML (letakk kan di file index.php)

<?php    
require_once 'function.php';    
?>
             <form action="" method="POST" enctype="multipart/form-data">
                <div class="modal-body">
                    <input type="hidden" name="id_user" value="<?php echo idUser($_SESSION['username']); ?>">
                    <div class="form-group">
                        <label for="">Jenis File</label>
                        <select name="id_jenis_file" id="slim-select">
                            <?php
                            $data = mysqli_query($conn"SELECT * FROM `tbl_jenis_file` ");
                             while ($row = mysqli_fetch_array($data)) {
                                 echo '<option value="' . $row['id_jenis_file'] . '">' . $row['jenis'] . '</option>';
                              }
                            ?>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="">Bentuk File</label>
                        <select name="bentuk" id="slim-select-two">
                            <?php
                            $bentuk = array("image""file");
                            foreach ($bentuk as $b) {
                                echo '<option value="' . $b . '">' . $b . '</option>';
                            }
                            ?>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="">File</label>
                        <input type="file" name="file" class="form-control" required />
                    </div>
                    Note : <span class="badge badge-info text-dark">Hanya Di Perboleh kan Format Jpg dan png svg , pdf ,word </span>
                </div>
                <div class="modal-footer">
                    <button type="submit" name="uploadBerkas" class="btn btn-primary">Upload File</button>
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                </div>
            </form>
Source Code Fungsi Menjalankan Form:
if (isset($_POST['uploadBerkas'])) {
    $id_user        = $_POST['id_user'];
    $id_jenis_file  = $_POST['id_jenis_file'];
    $bentuk         = $_POST['bentuk'];
    $ekstensi_diperbolehkan    = array('png''jpg''jpeg''svg''pdf''docx');
    $nama = $_FILES['file']['name'];
    $x = explode('.'$nama);
    $ekstensi = strtolower(end($x));
    $ukuran    = $_FILES['file']['size'];
    $file_tmp = $_FILES['file']['tmp_name'];

    if (in_array($ekstensi$ekstensi_diperbolehkan) === true) {
        if ($ukuran < 1044070) {
            // cek jenis file 
            $cekjenisFile = cekJenisFile($id_user$id_jenis_file);
            if (mysqli_num_rows($cekjenisFile) > 0) {
                $pesan = pesanDuplikat("Jenis File " . jenisFileId($id_jenis_file) . """Sudah Di Upload");
            } else {
                move_uploaded_file($file_tmp'file/' . $nama);
                $query = mysqli_query($conn"INSERT INTO `tbl_file` (`id_user`,`id_jenis_file`, `file`,`bentuk`)
                             VALUES('$id_user','$id_jenis_file','$nama','$bentuk')");
                if ($query) {
                    $pesan = pesanBerhasil("FILE BERHASIL DI UPLOAD");
                } else {
                    $pesan = pesanGagal("MENGUPLOAD GAMBAR");
                }
            }
        } else {
            $pesan = pesanGagal("UKURAN FILE TERLALU BESAR");
        }
    } else {
        $pesan = pesanGagal("EKSTENSI FILE YANG DI UPLOAD TIDAK DI PERBOLEHKAN");
    }
}


Source code file koneksi.php :

<?php
$conn = mysqli_connect('localhost''root''''upload');

Source Code Table nya untuk menampilkan hasil  di file index.php: 

        <div class="table-responsive">
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <th>No</th>
                        <th>Jenis</th>
                        <th>File</th>
                        <th>Bentuk File</th>
                        <th>Delete</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $file = mysqli_query($conn"SELECT * FROM `tbl_file` WHERE id_user='$idUser"); $no = 1;
                    while ($rowFile = mysqli_fetch_array($file)) {
                        echo '<tr>
                        <td>' . $no . '</td>
                        <td>' . jenisFileId($rowFile['id_jenis_file']) . '</td>
                        <td>' . cekBentukFile($rowFile['bentuk'], $rowFile['file']) . '</td>
                        <td>' . $rowFile['bentuk'] . '</td>
                        <td>' . linkDelete("deleteFile"$rowFile['id_file']) . '</td>
                        </tr>';
                        $no++;
                    }
                    ?>
                </tbody>
            </table>

kemudian buatlah satu file lagi yaitu file function.php (file ini untuk meletakkkan fungsi)

function.php
<?php
include_once 'koneksi.php';
// pesan gagal
  function pesanBerhasil($tipe)
  {
    $data = "<div class='alert alert-success alert-dismissible fade show' role='alert'>
    <strong>Berhasil !</strong> $tipe.
    <button type='button' class='close' data-dismiss='alert' aria-label='Close'>
      <span aria-hidden='true'>&times;</span>
    </button>
  </div>";
    return $data;
  }

  // pesan gagal
  function pesanGagal($tipe)
  {
    $data = "<div class='alert alert-danger alert-dismissible fade show' role='alert'>
    <strong>Maaf Anda Gagal !</strong> $tipe.
    <button type='button' class='close' data-dismiss='alert' aria-label='Close'>
      <span aria-hidden='true'>&times;</span>
    </button>
  </div>";
    return $data;
  }

// id jenis file
  function jenisFileId($id_jenis_file)
  {
    global $conn;
    $jenis = mysqli_query($conn"SELECT * FROM `tbl_jenis_file` WHERE id_jenis_file='$id_jenis_file");
    $rowJenisFile = mysqli_fetch_array($jenis);
    return $rowJenisFile['jenis'];
  }

// cek bentuk 
  function cekBentukFile($bentuk$file)
  {
    if ($bentuk == "image") {
      $data = "<img src='file/$file' width='120px' height='120px'>";
    } else {
      $data = $file . "<br>" . "<a target='blank' href='download.php?file=$file' class='badge badge-success'><span class='iconify' data-icon='ant-design:download-outlined'></span> Download</a>";
    }
    return $data;
  }


 

Semoga Bermanfaat !!