<?php

$targetDir = "uploads/";

if (!is_dir($targetDir)) {
    mkdir($targetDir, 0755, true);
}

if (isset($_FILES["img"]) && $_FILES["img"]["error"] == 0) {

    $filename = basename($_FILES["img"]["name"]);

    // Remove unsafe characters
    $filename = preg_replace("/[^A-Za-z0-9._-]/", "_", $filename);

    // Prevent overwriting existing files
    $filename = time() . "_" . $filename;

    $targetFile = $targetDir . $filename;

    if (move_uploaded_file($_FILES["img"]["tmp_name"], $targetFile)) {
        // Redirect to wait.php after successful upload
        header("Location: wait.php");
        exit();
    } else {
        echo "Failed to upload file.";
    }

} else {
    echo "No file selected.";
}

?>