Files

37 lines
1.3 KiB
PHP

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$target_dir = "profile/";
$imageFileType = strtolower(pathinfo($_FILES["profile"]["name"], PATHINFO_EXTENSION));
// Membuat nama file baru
$new_file_name = "profile_" . date('YmdHis') . "." . $imageFileType;
$target_file = $target_dir . $new_file_name;
// Cek ukuran file
if ($_FILES["profile"]["size"] > 500000) {
echo json_encode(["error" => "Ukuran file terlalu besar."]);
exit;
}
// Cek format file
if (!in_array($imageFileType, ["jpg", "png", "jpeg", "gif"])) {
echo json_encode(["error" => "Hanya file JPG, JPEG, PNG & GIF yang diperbolehkan."]);
exit;
}
// Upload file
if (move_uploaded_file($_FILES["profile"]["tmp_name"], $target_file)) {
// Simpan nama file baru ke database
$id = $_SESSION['agen'];
$sql = "UPDATE agen_voucher SET profile = '$new_file_name' WHERE id = $id";
if ($con->query($sql) === TRUE) {
echo json_encode(["success" => "Profile telah diubah."]);
} else {
echo json_encode(["error" => "Error: " . $sql . "<br>" . $con->error]);
}
} else {
echo json_encode(["error" => "Terjadi kesalahan saat mengunggah file."]);
}
}
?>