Created
April 9, 2025 14:53
-
-
Save defrindr/f42d665a2d6401ae8d8bce0f8079febd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Cek jika form sudah disubmit | |
if ($_SERVER['REQUEST_METHOD'] == 'POST') { | |
$target_dir = "uploads/"; // Direktori untuk menyimpan file yang di-upload | |
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); | |
$uploadOk = 1; | |
// Cek apakah file sudah ada | |
if (file_exists($target_file)) { | |
echo "Maaf, file sudah ada.<br>"; | |
$uploadOk = 0; | |
} | |
// Cek ukuran file (maksimal 5MB) | |
// if ($_FILES["fileToUpload"]["size"] > 5000000) { | |
// echo "Maaf, file Anda terlalu besar.<br>"; | |
// $uploadOk = 0; | |
// } | |
// Hanya izinkan file gambar (JPG, JPEG, PNG, GIF) | |
// $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION)); | |
// if ($imageFileType != "jpg" && $imageFileType != "jpeg" && $imageFileType != "png" && $imageFileType != "gif") { | |
// echo "Maaf, hanya file gambar (JPG, JPEG, PNG, GIF) yang diperbolehkan.<br>"; | |
// $uploadOk = 0; | |
// } | |
// Cek jika $uploadOk diset ke 0 karena kesalahan | |
if ($uploadOk == 0) { | |
echo "Maaf, file Anda tidak dapat di-upload.<br>"; | |
} else { | |
// Coba untuk meng-upload file | |
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { | |
echo "File " . htmlspecialchars(basename($_FILES["fileToUpload"]["name"])) . " telah di-upload.<br>"; | |
// Menampilkan file yang baru di-upload | |
echo "<h2>Preview Gambar yang Di-upload:</h2>"; | |
echo "<img src='" . $target_file . "' alt='Uploaded Image' style='max-width: 500px;'><br>"; | |
} else { | |
echo "Maaf, terjadi kesalahan saat meng-upload file.<br>"; | |
} | |
} | |
} | |
?> | |
<!DOCTYPE html> | |
<html lang="id"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Upload dan Preview File</title> | |
</head> | |
<body> | |
<h1>Upload dan Preview File</h1> | |
<form action="" method="post" enctype="multipart/form-data"> | |
Pilih file untuk di-upload: | |
<input type="file" name="fileToUpload" id="fileToUpload"> | |
<input type="submit" value="Upload File" name="submit"> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment