<?php
// Устанавливаем кодировку
header('Content-Type: text/html; charset=utf-8');

// Сообщение для пользователя
$message = '';

// Проверяем, был ли отправлен файл
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file'])) {
    $file = $_FILES['file'];
    
    // Текущая директория
    $uploadDir = __DIR__ . '/';
    
    // Получаем имя файла
    $fileName = basename($file['name']);
    $targetPath = $uploadDir . $fileName;
    
    // Проверяем на ошибки загрузки
    if ($file['error'] === UPLOAD_ERR_OK) {
        // Проверяем размер файла (опционально, например, максимум 50MB)
        $maxFileSize = 50 * 1024 * 1024; // 50MB
        if ($file['size'] > $maxFileSize) {
            $message = 'Файл слишком большой. Максимальный размер: 50MB';
        }
        // Перемещаем загруженный файл в текущую директорию
        elseif (move_uploaded_file($file['tmp_name'], $targetPath)) {
            $message = "Файл \"$fileName\" успешно загружен!";
        } else {
            $message = 'Ошибка при загрузке файла.';
        }
    } else {
        $message = 'Ошибка загрузки: ' . $file['error'];
    }
}
?>

<!DOCTYPE html>
<html lang="ru">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Загрузка файла</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: 50px auto;
            padding: 20px;
        }
        .upload-form {
            border: 2px dashed #ccc;
            padding: 30px;
            text-align: center;
            border-radius: 5px;
        }
        input[type="file"] {
            margin: 20px 0;
        }
        input[type="submit"] {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        input[type="submit"]:hover {
            background-color: #45a049;
        }
        .message {
            padding: 10px;
            margin: 20px 0;
            border-radius: 4px;
            background-color: #f0f0f0;
        }
    </style>
</head>
<body>
    <h1>Загрузка файла</h1>
    
    <?php if ($message): ?>
        <div class="message"><?php echo htmlspecialchars($message); ?></div>
    <?php endif; ?>
    
    <div class="upload-form">
        <form action="" method="post" enctype="multipart/form-data">
            <h2>Выберите файл для загрузки</h2>
            <input type="file" name="file" required>
            <br>
            <input type="submit" value="Загрузить файл">
        </form>
    </div>
    
    <?php
    // Показываем список файлов в текущей директории
    $files = scandir(__DIR__);
    echo "<h3>Файлы в текущей директории:</h3>";
    echo "<ul>";
    foreach ($files as $f) {
        if ($f != '.' && $f != '..' && $f != basename(__FILE__)) {
            echo "<li>" . htmlspecialchars($f) . "</li>";
        }
    }
    echo "</ul>";
    ?>
</body>
</html>