<?php

// $targetDir = "uploads/";

// // Create folder if not exists
// if (!is_dir($targetDir)) {
//     mkdir($targetDir, 0755, true);
// }

// if (isset($_POST["submit"])) {

//     $fileName = basename($_FILES["fileToUpload"]["name"]);
//     $targetFile = $targetDir . $fileName;
//     $fileType = strtolower(pathinfo($targetFile, PATHINFO_EXTENSION));

//     // Validation
//     $allowedTypes = ["jpg", "jpeg", "png", "gif", "pdf"];

//     if (!in_array($fileType, $allowedTypes)) {
//         echo "Only JPG, JPEG, PNG, GIF & PDF files are allowed.";
//         exit;
//     }

//     // Check file size (5MB max)
//     if ($_FILES["fileToUpload"]["size"] > 5 * 1024 * 1024) {
//         echo "File is too large.";
//         exit;
//     }

//     // Upload file
//     if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $targetFile)) {
//         echo "File uploaded successfully: " . $fileName;
//     } else {
//         echo "Error uploading file.";
//     }
// }

?>

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
ini_set('upload_tmp_dir', __DIR__ . '/tmp');
echo __DIR__;
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    echo "hii";
    echo "<pre>";
print_r($_FILES);
echo "</pre>";
    if (isset($_FILES['file']) && $_FILES['file']['error'] == 0) {

        $uploadDir = __DIR__ . '/uploads/';
        // folder create if not exists
        if (!is_dir($uploadDir)) {
            mkdir($uploadDir, 0777, true);
        }

        $fileName = time() . '_' . basename($_FILES['file']['name']);
        $targetFile = $uploadDir . $fileName;
        echo sys_get_temp_dir();
        if (move_uploaded_file($_FILES['file']['tmp_name'], $targetFile)) {
            echo "✅ File uploaded successfully!";
        } else {
            echo "❌ Upload failed!";
        }

    } else {
        echo "❌ No file selected or upload error!";
    }
}
?>

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

<h2>Upload File</h2>

<form method="POST" enctype="multipart/form-data">
    <input type="file" name="file" required>
    <br><br>
    <button type="submit">Upload</button>
</form>

</body>
</html>