<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json");

$upload_dir = "../uploads/posts/";
if (!is_dir($upload_dir)) {
    mkdir($upload_dir, 0777, true);
}

if (isset($_FILES['post_image'])) {
    $image_name = time() . "_" . basename($_FILES["post_image"]["name"]);
    $target = $upload_dir . $image_name;

    if (move_uploaded_file($_FILES["post_image"]["tmp_name"], $target)) {
        echo json_encode([
            "success" => true,
            "image_url" => "http://schoolmata.com.ng/mobile/api/uploads/posts/" . $image_name
        ]);
    } else {
        echo json_encode(["success" => false, "message" => "Failed to move file"]);
    }
} else {
    echo json_encode(["success" => false, "message" => "No image uploaded"]);
}
