first commit
This commit is contained in:
53
image_upload.php
Normal file
53
image_upload.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
// image_upload.php
|
||||
session_start();
|
||||
|
||||
// Проверка прав администратора
|
||||
if (!isset($_SESSION['isAdmin']) || $_SESSION['isAdmin'] !== true) {
|
||||
echo json_encode(['success' => false, 'message' => 'Доступ запрещен']);
|
||||
exit();
|
||||
}
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['image'])) {
|
||||
$uploadDir = 'uploads/products/';
|
||||
|
||||
// Создаем директорию если не существует
|
||||
if (!file_exists($uploadDir)) {
|
||||
mkdir($uploadDir, 0777, true);
|
||||
}
|
||||
|
||||
$allowedTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
|
||||
$maxSize = 5 * 1024 * 1024; // 5MB
|
||||
|
||||
$file = $_FILES['image'];
|
||||
|
||||
// Проверка типа файла
|
||||
if (!in_array($file['type'], $allowedTypes)) {
|
||||
echo json_encode(['success' => false, 'message' => 'Допустимые форматы: JPEG, PNG, GIF, WebP']);
|
||||
exit();
|
||||
}
|
||||
|
||||
// Проверка размера
|
||||
if ($file['size'] > $maxSize) {
|
||||
echo json_encode(['success' => false, 'message' => 'Максимальный размер файла: 5MB']);
|
||||
exit();
|
||||
}
|
||||
|
||||
// Генерируем уникальное имя
|
||||
$extension = pathinfo($file['name'], PATHINFO_EXTENSION);
|
||||
$fileName = 'product_' . time() . '_' . rand(1000, 9999) . '.' . $extension;
|
||||
$filePath = $uploadDir . $fileName;
|
||||
|
||||
if (move_uploaded_file($file['tmp_name'], $filePath)) {
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'url' => $filePath,
|
||||
'name' => $fileName
|
||||
]);
|
||||
} else {
|
||||
echo json_encode(['success' => false, 'message' => 'Ошибка загрузки файла']);
|
||||
}
|
||||
} else {
|
||||
echo json_encode(['success' => false, 'message' => 'Файл не получен']);
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user