File Manager V1.5
FILE_CONTENT: upload_image.php
<?php
require_once 'db.php';
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, PATCH, OPTIONS");
header("Access-Control-Allow-Headers: Authorization, Content-Type, Accept, X-Requested-With, User-Agent, Origin");
header("Access-Control-Max-Age: 86400");
header("Content-Type: application/json");
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(204);
exit;
}
// Используем универсальную функцию для получения токена
$token = getAuthToken();
if (!$token) {
http_response_code(401);
echo json_encode(['success' => false, 'error' => 'Authorization required']);
exit;
}
$decoded = verifyToken($token);
if (!$decoded) {
http_response_code(401);
echo json_encode(['success' => false, 'error' => 'Invalid token']);
exit;
}
$target = $_GET['target'] ?? 'main';
$userId = $_GET['user_id'] ?? null;
$userType = $_GET['user_type'] ?? null;
file_put_contents('debug.log', "Received: user_id=$userId, user_type=$userType, target=$target\n", FILE_APPEND);
file_put_contents('debug.log', print_r($_FILES, true) . "\n", FILE_APPEND);
if (!$userId || !$userType) {
http_response_code(400);
echo json_encode(['success' => false, 'error' => 'Missing user_id or user_type']);
exit;
}
$uploadDir = __DIR__ . '/uploads/' . ($target === 'backup' ? 'backup/' : 'main/');
if (!file_exists($uploadDir)) {
if (!mkdir($uploadDir, 0777, true)) {
http_response_code(500);
echo json_encode(['success' => false, 'error' => 'Failed to create upload directory']);
exit;
}
}
if (!is_writable($uploadDir)) {
http_response_code(500);
echo json_encode(['success' => false, 'error' => 'Upload directory is not writable']);
exit;
}
if (!isset($_FILES['image'])) {
http_response_code(400);
echo json_encode(['success' => false, 'error' => 'No image uploaded']);
exit;
}
$file = $_FILES['image'];
$fileName = uniqid() . '_' . $file['name'];
$filePath = $uploadDir . $fileName;
$allowedTypes = ['image/jpeg', 'image/jpg', 'image/png'];
if (!in_array($file['type'], $allowedTypes)) {
http_response_code(400);
echo json_encode(['success' => false, 'error' => 'Invalid file type. Only JPG, JPEG, PNG are allowed']);
exit;
}
if ($file['size'] > 5 * 1024 * 1024) {
http_response_code(400);
echo json_encode(['success' => false, 'error' => 'File too large. Maximum size is 5MB']);
exit;
}
if (move_uploaded_file($file['tmp_name'], $filePath)) {
$url = 'http://' . $_SERVER['HTTP_HOST'] . '/uploads/' . ($target === 'backup' ? 'backup/' : 'main/') . $fileName;
echo json_encode([
'success' => true,
'url' => $url,
'user_id' => $userId,
'user_type' => $userType
]);
} else {
http_response_code(500);
echo json_encode(['success' => false, 'error' => 'Failed to upload image']);
}
?>[ KEMBALI ]