rasyid03

tesupload

Oct 16th, 2023
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Form Upload File</title>
  5. </head>
  6. <body>
  7. <h2>Form Upload File</h2>
  8. <form action="upload.php" method="POST" enctype="multipart/form-data">
  9. <input type="file" name="fileToUpload" id="fileToUpload">
  10. <input type="submit" value="Upload File" name="submit">
  11. </form>
  12. </body>
  13. </html>
  14.  
  15. <?php
  16. if(isset($_POST["submit"])) {
  17. $targetDirectory = "uploads/"; // Direktori tempat file akan disimpan
  18. $targetFile = $targetDirectory . basename($_FILES["fileToUpload"]["name"]);
  19. $uploadOk = 1;
  20.  
  21. // Periksa apakah file sudah ada
  22. if (file_exists($targetFile)) {
  23. echo "Maaf, file tersebut sudah ada.";
  24. $uploadOk = 0;
  25. }
  26.  
  27. // Periksa ukuran file
  28. if ($_FILES["fileToUpload"]["size"] > 500000) { // Di sini, batas ukuran adalah 500KB
  29. echo "Maaf, ukuran file terlalu besar.";
  30. $uploadOk = 0;
  31. }
  32.  
  33. // Izinkan hanya beberapa jenis file tertentu (contoh: hanya izinkan gambar)
  34. $allowedFileTypes = array("jpg", "jpeg", "png", "gif");
  35. $fileExtension = strtolower(pathinfo($targetFile, PATHINFO_EXTENSION));
  36. if (!in_array($fileExtension, $allowedFileTypes)) {
  37. echo "Maaf, hanya file JPG, JPEG, PNG, dan GIF yang diizinkan.";
  38. $uploadOk = 0;
  39. }
  40.  
  41. // Cek apakah ada kesalahan lain selama unggahan
  42. if ($uploadOk == 0) {
  43. echo "Maaf, file Anda tidak diunggah.";
  44. } else {
  45. // Jika semua pemeriksaan berhasil, unggah file
  46. if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $targetFile)) {
  47. echo "File " . htmlspecialchars(basename($_FILES["fileToUpload"]["name"])) . " berhasil diunggah.";
  48. } else {
  49. echo "Maaf, terjadi kesalahan saat mengunggah file.";
  50. }
  51. }
  52. }
  53. ?>
  54.  
  55.  
Add Comment
Please, Sign In to add comment