Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- session_name("Asteroid2Id");
- session_start();
- $base_uri = dirname($_SERVER["PHP_SELF"]);
- if($base_uri=='/')
- $base_uri='';
- if (! isset($_SESSION['userid']) || empty($_SESSION['admin'])) {
- header("Location: {$base_uri}/?do=login");
- exit;
- }
- $typesDir = __DIR__ . "/images/types/";
- $jsonPath = $typesDir . "skins.json";
- $typeCodes = [
- "TL" => "Светофор",
- "G" => "Пешеходный переход"
- ];
- if (!file_exists($jsonPath)) {
- $initial = [];
- foreach (array_keys($typeCodes) as $code) {
- $initial[$code] = $code . ".png";
- }
- file_put_contents($jsonPath, json_encode($initial, JSON_PRETTY_PRINT));
- }
- $error = "";
- $success = "";
- if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['type_code'])) {
- $type = $_POST['type_code'];
- if (!array_key_exists($type, $typeCodes)) {
- $error = "Неизвестный код типа.";
- }
- else if (empty($_FILES['skin_file']) || $_FILES['skin_file']['error'] !== UPLOAD_ERR_OK) {
- $error = "Ошибка при загрузке файла.";
- }
- else {
- $allowed = ['png'];
- $origName = $_FILES['skin_file']['name'];
- $ext = strtolower(pathinfo($origName, PATHINFO_EXTENSION));
- if (!in_array($ext, $allowed)) {
- $error = "Разрешены только форматы PNG";
- } else {
- if (!is_dir($typesDir)) mkdir($typesDir, 0755, true);
- $newFilename = $type . "." . $ext;
- $targetPath = $typesDir . $newFilename;
- if (move_uploaded_file($_FILES['skin_file']['tmp_name'], $targetPath)) {
- $skins = json_decode(file_get_contents($jsonPath), true);
- $skins[$type] = $newFilename;
- file_put_contents($jsonPath, json_encode($skins, JSON_PRETTY_PRINT));
- $success = "Картинка для типа «{$typeCodes[$type]}» успешно загружена.";
- } else {
- $error = "Не удалось сохранить файл на сервер.";
- }
- }
- }
- }
- $skins = json_decode(file_get_contents($jsonPath), true);
- ?><!DOCTYPE html>
- <html lang="ru">
- <head>
- <meta charset="UTF-8">
- <title>Управление фонами типов устройств</title>
- <link href="css/bootstrap.min.css" type="text/css" rel="stylesheet">
- <style>
- body { padding: 20px; }
- img.skin-preview { max-height: 80px; margin-bottom: 5px; }
- </style>
- </head>
- <body>
- <h3>Настройка фоновых изображений для типов устройств</h3>
- <?php if ($error): ?>
- <div class="alert alert-danger"><?= htmlspecialchars($error) ?></div>
- <?php endif; ?>
- <?php if ($success): ?>
- <div class="alert alert-success"><?= htmlspecialchars($success) ?></div>
- <?php endif; ?>
- <table class="table table-bordered">
- <thead class="thead-light">
- <tr>
- <th>Код типа</th>
- <th>Название</th>
- <th>Текущий файл</th>
- <th>Загрузка новой картинки</th>
- </tr>
- </thead>
- <tbody>
- <?php foreach ($typeCodes as $code => $label):
- $currFile = $skins[$code] ?? ($code . ".png");
- $imgPath = "images/types/" . $currFile;
- ?>
- <tr>
- <td><strong><?= htmlspecialchars($code) ?></strong></td>
- <td><?= htmlspecialchars($label) ?></td>
- <td>
- <?php if (file_exists($typesDir . $currFile)): ?>
- <img class="skin-preview" src="<?= htmlspecialchars($imgPath) ?>" alt="<?= htmlspecialchars($label) ?>">
- <br><?= htmlspecialchars($currFile) ?>
- <?php else: ?>
- <em>Файл «<?= htmlspecialchars($currFile) ?>» не найден</em>
- <?php endif; ?>
- </td>
- <td>
- <form action="" method="post" enctype="multipart/form-data" class="form-inline">
- <input type="hidden" name="type_code" value="<?= htmlspecialchars($code) ?>">
- <div class="form-group mb-2">
- <input type="file" name="skin_file" accept="image/*" required class="form-control-file">
- </div>
- <button type="submit" class="btn btn-sm btn-primary mb-2 ml-2">Загрузить</button>
- </form>
- </td>
- </tr>
- <?php endforeach; ?>
- </tbody>
- </table>
- <script src="js/bootstrap.bundle.min.js"></script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement