Guest User

Untitled

a guest
Mar 13th, 2024
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. <?php
  2. // Настройки
  3. $uploadDir = 'uploads/';
  4. $allowedExtensions = ['jpg', 'jpeg', 'png', 'gif'];
  5. $maxFileSize = 5 * 1024 * 1024; // 5 MB
  6.  
  7. // Обработка загрузки файла
  8. if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['image'])) {
  9. $file = $_FILES['image'];
  10. $fileName = $file['name'];
  11. $fileTmpPath = $file['tmp_name'];
  12. $fileSize = $file['size'];
  13. $fileExtension = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
  14.  
  15. if (in_array($fileExtension, $allowedExtensions) && $fileSize <= $maxFileSize) {
  16. $newFileName = uniqid() . '.' . $fileExtension;
  17. $destinationPath = $uploadDir . $newFileName;
  18. move_uploaded_file($fileTmpPath, $destinationPath);
  19. }
  20. }
  21.  
  22. // Получение списка загруженных файлов
  23. $files = glob($uploadDir . '*');
  24. ?>
  25.  
  26. <!DOCTYPE html>
  27. <html>
  28. <head>
  29. <title>Простая имиджборда</title>
  30. </head>
  31. <body>
  32. <h1>Простая имиджборда</h1>
  33.  
  34. <form action="" method="post" enctype="multipart/form-data">
  35. <input type="file" name="image" accept="image/*" required>
  36. <button type="submit">Загрузить</button>
  37. </form>
  38.  
  39. <div>
  40. <?php foreach ($files as $file): ?>
  41. <img src="<?= $file ?>" alt="Загруженное изображение" style="max-width: 200px; max-height: 200px;">
  42. <?php endforeach; ?>
  43. </div>
  44. </body>
  45. </html>
  46.  
Advertisement
Add Comment
Please, Sign In to add comment