Upload Photo

Cara upload photo

<?php include __DIR__ . '/../../includes/header.php'; ?>
<?php require __DIR__ . '/../../includes/config.php'; ?>

<div class="container mt-5">
    <h1 class="mb-4">Tambah Foto</h1>

    <?php
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $baseUrl = $protocol . $host;
        $judulFoto = $_POST['JudulFoto'];
        $descripsiFoto = $_POST['DescripsiFoto'];
        $tanggalUnggah = $_POST['TanggalUnggah'];
        $userID = $_SESSION['UserID'];
        $lokasiFile = null;
        $message = '';

        if (!empty($_FILES['LokasiFile']['name'])) {
            $baseDir = "/assets/img/";
            $targetDir = $_SERVER['DOCUMENT_ROOT'] . $baseDir;
            $targetFile = $targetDir . basename($_FILES["LokasiFile"]["name"]);
            $imageFileType = strtolower(pathinfo($targetFile, PATHINFO_EXTENSION));
    
            $allowedFileTypes = ['jpg', 'jpeg', 'png', 'gif'];
    
            $check = getimagesize($_FILES["LokasiFile"]["tmp_name"]);
            if ($check !== false) {
                if (in_array($imageFileType, $allowedFileTypes)) {
                    if (move_uploaded_file($_FILES["LokasiFile"]["tmp_name"], $targetFile)) {
                        $lokasiFile = $baseDir . '/' . basename($_FILES["LokasiFile"]["name"]);
                        $message = "<div class='alert alert-success'>File berhasil di-upload: $lokasiFile</div>";
                    } else {
                        $message = "<div class='alert alert-danger'>Maaf, terjadi kesalahan saat meng-upload foto.</div>";
                    }
                } else {
                    $message = "<div class='alert alert-danger'>Hanya file JPG, JPEG, PNG, dan GIF yang diperbolehkan.</div>";
                }
            } else {
                $message = "<div class='alert alert-danger'>File yang di-upload bukan gambar.</div>";
            }
        } elseif (!empty($_POST['UrlFile'])) {
            $lokasiFile = $_POST['UrlFile'];
        }

        // Simpan ke database
        if ($lokasiFile) {
            $sql = "INSERT INTO foto (JudulFoto, DescripsiFoto, TanggalUnggah, LokasiFile, UserID) VALUES (:JudulFoto, :DescripsiFoto, :TanggalUnggah, :LokasiFile, :UserID)";
            $stmt = $pdo->prepare($sql);
            $stmt->bindParam(':JudulFoto', $judulFoto);
            $stmt->bindParam(':DescripsiFoto', $descripsiFoto);
            $stmt->bindParam(':TanggalUnggah', $tanggalUnggah);
            $stmt->bindParam(':LokasiFile', $lokasiFile);
            $stmt->bindParam(':UserID', $userID);

            if ($stmt->execute()) {
                $message = "<div class='alert alert-success'>Foto berhasil ditambahkan!</div>";
                // Tambahkan script untuk redirect setelah 3 detik
                $redirect = "<script>
                    setTimeout(function() {
                        window.location.href = 'index.php'; // Ganti dengan path yang sesuai
                    }, 3000);
                </script>";
            } else {
                $message = "<div class='alert alert-danger'>Gagal menambahkan foto ke database.</div>";
            }
        }

        // Tampilkan pesan jika ada
        if (!empty($message)) {
            echo "<div class='alert-container' style='position: relative; top: -20px; z-index: 1000;'>
                    $message
                  </div>";
            if (isset($redirect)) {
                echo $redirect; // Tampilkan script redirect jika ada
            }
        }
    }
    ?>

    <form action="" method="post" enctype="multipart/form-data">
        <div class="mb-3">
            <label for="judulFoto" class="form-label">Judul Foto</label>
            <input type="text" name="JudulFoto" id="judulFoto" class="form-control" required>
        </div>
        <div class="mb-3">
            <label for="descripsiFoto" class="form-label">Deskripsi Foto</label>
            <textarea name="DescripsiFoto" id="descripsiFoto" class="form-control" rows="3"></textarea>
        </div>
        <div class="mb-3">
            <label for="tanggalUnggah" class="form-label">Tanggal Unggah</label>
            <input type="date" name="TanggalUnggah" id="tanggalUnggah" class="form-control" value="<?php echo date('Y-m-d'); ?>" readonly>
        </div>
        <div class="mb-3">
            <label for="lokasiFile" class="form-label">Upload Foto (dari komputer)</label>
            <input type="file" name="LokasiFile" id="lokasiFile" class="form-control" accept="image/*">
        </div>
        <div class="mb-3">
            <label for="urlFile" class="form-label">Atau masukkan URL Foto</label>
            <input type="url" name="UrlFile" id="urlFile" class="form-control" placeholder="http://example.com/foto.jpg">
        </div>
        <button type="submit" class="btn btn-primary">Upload Foto</button>
    </form>
</div>

<?php include __DIR__ . '/../../includes/footer.php'; ?>

Last updated