Bagaimana cara membuat Script upload image dengan php ?
Bagaimana cara membuat Script upload image dengan php ? mungkin menjadi salah satu pertanyaan anda ketika belajar atau menggunakan php.Untuk itu mari kita bahas bersama - sama cara membuat Script upload image dengan php.
- Buat html untuk memilih file yang akan di upload
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
 Pilih image yang akan di upload:
  <input type="file" name="fileToUpload" id="fileToUpload">
 <input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>Â - Buat file upload.php untuk memproses upload dari file yang di pilih
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check apakah image atau bukan
if(isset($_POST["submit"])) {
  $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
  if($check !== false) {
   echo "File yang di upload adalah image - " . $check["mime"] . ".";
   $uploadOk = 1;
  } else {
   echo "File bukan image.";
   $uploadOk = 0;
  }
// Check ukuran file
if ($_FILES["fileToUpload"]["size"] > 500000) {
  echo "Maaf file anda terlalu besar.";
  $uploadOk = 0;
}
// Limit type file yang boleh di upload
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
  echo "Maaf hanya JPG, JPEG, PNG & GIF yang boleh digunakan.";
  $uploadOk = 0;
}
// Check apakah ada eror dari fungsi pengecekan di atas
if ($uploadOk == 0) {
  echo "Sorry, your file was not uploaded.";
// jika sudah terpenuhi semua jalan fungsi upload
} else {
 if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
   echo "foto ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " berhasil di upload.";
 } else {
   echo "Maaf upload foto gagal silahkan coba kembali.";
  }
}}
?>
Mudah bukan membuat Script upload image dengan php ? script di atas dapat anda kembangkan lagi sesuai denga kebutuhan anda. Happy Coding :)