<?php
// Handle file upload when the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $target_dir = "uploads/";
    $upload_ok = true;

    // Ensure uploads directory exists
    if (!is_dir($target_dir)) {
        mkdir($target_dir, 0755, true);
    }

    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

    // You can add more validation here (e.g., file size/type)

    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        $message = "✅ File uploaded: " . htmlspecialchars(basename($_FILES["fileToUpload"]["name"]));
        $download_link = $target_file;
    } else {
        $message = "❌ Sorry, there was an error uploading your file.";
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>File Upload</title>
</head>
<body>

<h2>Upload a File</h2>

<!-- Display upload status message -->
<?php if (isset($message)): ?>
    <p><?php echo $message; ?></p>
    <?php if (isset($download_link)): ?>
        <p><a href="<?php echo $download_link; ?>">📥 Download File</a></p>
    <?php endif; ?>
<?php endif; ?>

<!-- Upload form -->
<form action="" method="post" enctype="multipart/form-data">
    <label>Select file to upload:</label><br>
    <input type="file" name="fileToUpload" required><br><br>
    <input type="submit" value="Upload File">
</form>

</body>
</html>
