File Manager V1.5
FILE_CONTENT: upload_passports.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;
}
$userId = $_POST['user_id'] ?? null;
$userType = $_POST['user_type'] ?? null;
file_put_contents('debug.log', "Received: user_id=$userId, user_type=$userType\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/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;
}
$allowedTypes = ['image/jpeg', 'image/jpg', 'image/png'];
$maxFileSize = 5 * 1024 * 1024;
$uploadedFiles = [];
$errors = [];
$filesToProcess = [
'passport_main_image' => 'passport_main',
'passport_registration_image' => 'passport_registration',
'company_card_image' => 'company_card'
];
foreach ($filesToProcess as $field => $prefix) {
if (isset($_FILES[$field]) && $_FILES[$field]['error'] !== UPLOAD_ERR_NO_FILE) {
$file = $_FILES[$field];
if (!in_array($file['type'], $allowedTypes)) {
$errors[] = "Invalid file type for $field. Only JPG, JPEG, PNG are allowed";
continue;
}
if ($file['size'] > $maxFileSize) {
$errors[] = "File $field too large. Maximum size is 5MB";
continue;
}
$fileName = uniqid($prefix . '_') . '_' . $file['name'];
$filePath = $uploadDir . $fileName;
if (move_uploaded_file($file['tmp_name'], $filePath)) {
$url = 'http://' . $_SERVER['HTTP_HOST'] . '/uploads/main/' . $fileName;
$uploadedFiles[$field] = $url;
} else {
$errors[] = "Failed to upload $field";
}
}
}
if (!empty($errors)) {
http_response_code(400);
echo json_encode(['success' => false, 'error' => implode('; ', $errors)]);
exit;
}
if (empty($uploadedFiles)) {
http_response_code(400);
echo json_encode(['success' => false, 'error' => 'No valid files uploaded']);
exit;
}
echo json_encode([
'success' => true,
'user_id' => $userId,
'user_type' => $userType,
'uploaded_files' => $uploadedFiles
]);
?>[ KEMBALI ]