HIMANIFO UNIMMA
 Membuat Fitur Upload File di PHP Web Programming 

16:00:00, 14 January 2021

Web Programming

MEMBUAT FITUR UPLOAD FILE DI PHP

Upload file merupakan kegiatan pengiriman file dari client (pengunjung web) ke server.

Fitur Upload File memerlukan 2 hal, yaitu:
1. Kode Skrip untuk Formulir Upload File
2. Kode Skrip untuk menerima dan memproses file yang di Upload

1. Membuat Formulir Upload File

Berikut contoh pembuatan formulir upload file:

<!DOCTYPE html>
<html>
<head>
<title> Formulir Upload File di PHP </title>
</head>
<body>
<form action="penanganan_upload.php" method="post" enctype="multipart/form-data">
  <label for="file_anda"> Select File: </label>
  <input type="file" name="file_anda" accept="image/*"/>
  <input type="submit" value="Submit (Upload)" name="submit"/>
</form>
</body>
</html>

2. Membuat Penanganan Upload File

Berikut contoh penanganan upload file:

<!DOCTYPE html>
<html>
<head>
<title> Penanganan Upload File di PHP </title>
</head>
<body>
<?php
$upload_ok = 1;
$target_dir = "uploads/";

// sudah klik submit
if(isset($_POST["submit"])) {

// check upload_ok
if ($upload_ok == 1){
// check file name
$target_file = $target_dir . basename($_FILES["file_anda"]["name"]);
if (file_exists($target_file)) {
  echo "Maaf, file sudah ada.";
  echo "<br>";
  $upload_ok = 0;

}

// check upload_ok
if ($upload_ok == 1){
// check file size
if ($_FILES["file_anda"]["size"] > 500000) {
  echo "Maaf, file terlalu besar.";
  echo "<br>";
  $upload_ok = 0;
}
}

// check upload_ok
if ($upload_ok == 1){
// check file format
$format_file = strtolower( pathinfo($target_file, PATHINFO_EXTENSION) );
if($format_file != "jpg" && $format_file != "png" && $format_file != "jpeg"
&& $format_file != "gif" ) {
  echo "Maaf, format file tidak mendukung.";
  echo "<br>";
  $upload_ok = 0;
}
}

// check upload_ok
if ($upload_ok == 0) {
  echo "Maaf, file tidak terupload.";
} else {
  if (move_uploaded_file( $_FILES["file_anda"]["tmp_name"], $target_file )) {
    echo "File ". htmlspecialchars( basename( $_FILES["file_anda"]["name"] ) ). " telah terupload.";
    echo "<br>";
  } else {
    echo "Maaf, upload file gagal.";
    echo "<br>";
  }
}

// end of submit
}
?>
</body>

MUDAH KAN ?? SELANJUTNYA...

Membuat Cookies di PHP Web Programming | HIMANIFO UNIMMA →

Jika ada pertanyaan bisa gabung diskusi dibawah atau kontak langsung
Mail :
ajiwahyu12592@gmail.com

#Himanifo Unimma
#Membuat Fitur Upload File Di PHP
#Belajar Web Programming
#Himpunan Mahasiswa Jurusan Teknik Informatika

Gabung Diskusi