<?php
// Check if the form has been submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Check if file was uploaded without errors
    if (isset($_FILES['uploadedFile']) && $_FILES['uploadedFile']['error'] == 0) {
        // Assign variables for convenience
        $fileName = $_FILES['uploadedFile']['name'];
        $fileSize = $_FILES['uploadedFile']['size'];
        $fileTmpPath = $_FILES['uploadedFile']['tmp_name'];
        $fileType = $_FILES['uploadedFile']['type'];

        // Define the allowed file extensions
        $allowedFileTypes = ['jpg', 'jpeg', 'png', 'gif', 'pdf', 'txt', 'zip'];

        // Extract file extension
        $fileExtension = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));

        // Check if file extension is allowed
        if (in_array($fileExtension, $allowedFileTypes)) {
            // Specify the directory where the file will be saved
            $uploadDir = 'uploads/';
            $destFilePath = $uploadDir . $fileName;

            // Move the file to the specified directory
            if (move_uploaded_file($fileTmpPath, $destFilePath)) {
                echo "File uploaded successfully: " . $fileName;
            } else {
                echo "There was an error uploading the file.";
            }
        } else {
            echo "Upload failed. Only these file types are allowed: " . implode(', ', $allowedFileTypes);
        }
    } else {
        echo "Error: " . $_FILES['uploadedFile']['error'];
    }
}
?>

<!-- HTML form for file upload -->
<form action="" method="post" enctype="multipart/form-data">
    <p>
        <label for="fileSelect">Filename:</label>
        <input type="file" name="uploadedFile" id="fileSelect">
    </p>
    <p>
        <input type="submit" value="Upload">
    </p>
</form>
