Files
simple-file-share/upload_chunk.php
2025-06-13 14:59:47 +03:00

119 lines
4.0 KiB
PHP

<?php
// Включаем отображение всех ошибок для отладки
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('log_errors', 1);
session_start();
try {
error_log("=== UPLOAD CHUNK START ===");
error_log("POST data: " . print_r($_POST, true));
error_log("FILES data: " . print_r($_FILES, true));
error_log("Session data: " . print_r($_SESSION, true));
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/functions.php';
// Проверка авторизации
if (!isset($_SESSION['logged_in'])) {
error_log("ERROR: User not logged in");
http_response_code(403);
echo "Not authorized";
exit;
}
// Проверка CSRF токена
$csrf_token = $_POST['csrf_token'] ?? '';
error_log("CSRF token from POST: " . $csrf_token);
error_log("CSRF token from session: " . ($_SESSION['csrf_token'] ?? 'NOT SET'));
if (!verifyCSRFToken($csrf_token)) {
error_log("ERROR: CSRF token verification failed");
http_response_code(403);
echo "CSRF token verification failed";
exit;
}
// Проверка данных POST
if (!isset($_POST['filename']) || !isset($_POST['index'])) {
error_log("ERROR: Missing POST parameters");
http_response_code(400);
echo "Missing parameters";
exit;
}
// Проверка загруженного файла
if (!isset($_FILES['chunk']) || $_FILES['chunk']['error'] !== UPLOAD_ERR_OK) {
error_log("ERROR: File upload error. Error code: " . ($_FILES['chunk']['error'] ?? 'NO_FILE'));
http_response_code(400);
echo "File upload error: " . ($_FILES['chunk']['error'] ?? 'NO_FILE');
exit;
}
$filename = getSafeFileName($_POST['filename']);
$index = intval($_POST['index']);
error_log("Processing chunk: filename='$filename', index=$index");
// Валидация имени файла
if (!validateFileName($filename)) {
error_log("ERROR: Invalid filename: $filename");
http_response_code(400);
echo "Invalid filename";
exit;
}
// Создание директории для чанков
$tmpDir = CHUNK_DIR . $filename;
error_log("Chunk directory: $tmpDir");
if (!file_exists($tmpDir)) {
if (!mkdir($tmpDir, 0755, true)) {
error_log("ERROR: Cannot create chunk directory: $tmpDir");
http_response_code(500);
echo "Cannot create chunk directory";
exit;
}
error_log("Created chunk directory: $tmpDir");
}
// Проверка прав на запись
if (!is_writable($tmpDir)) {
error_log("ERROR: Chunk directory is not writable: $tmpDir");
http_response_code(500);
echo "Chunk directory is not writable";
exit;
}
$chunkPath = "$tmpDir/$index.part";
error_log("Chunk path: $chunkPath");
error_log("Temporary file: " . $_FILES['chunk']['tmp_name']);
error_log("Chunk size: " . $_FILES['chunk']['size']);
// Перемещение загруженного чанка
if (move_uploaded_file($_FILES['chunk']['tmp_name'], $chunkPath)) {
error_log("SUCCESS: Chunk saved to $chunkPath");
http_response_code(200);
echo "OK";
} else {
error_log("ERROR: Cannot move uploaded file to $chunkPath");
error_log("PHP error: " . error_get_last()['message'] ?? 'Unknown error');
http_response_code(500);
echo "Cannot save chunk";
}
error_log("=== UPLOAD CHUNK END ===");
} catch (Exception $e) {
error_log("EXCEPTION in upload_chunk.php: " . $e->getMessage());
error_log("Stack trace: " . $e->getTraceAsString());
http_response_code(500);
echo "Server error: " . $e->getMessage();
} catch (Error $e) {
error_log("FATAL ERROR in upload_chunk.php: " . $e->getMessage());
error_log("Stack trace: " . $e->getTraceAsString());
http_response_code(500);
echo "Fatal error: " . $e->getMessage();
}