Да будет код...

This commit is contained in:
2026-05-20 23:32:01 +03:00
commit b903cca6e2
4 changed files with 99 additions and 0 deletions

38
upload.php Normal file
View File

@@ -0,0 +1,38 @@
<?php
$uploadDir = __DIR__ . '/uploads/';
if (!is_dir($uploadDir)) mkdir($uploadDir, 0755, true);
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$fileChunk = $_FILES['file_chunk'];
$chunkIndex = (int)$_POST['chunk_index'];
$totalChunks = (int)$_POST['total_chunks'];
$originalFilename = basename($_POST['filename']);
// Временный файл для хранения склеиваемых частей
$tempFilePath = $uploadDir . $originalFilename . '.part';
// Открываем временный файл для дозаписи (режим append)
$out = fopen($tempFilePath, 'ab');
if ($out) {
// Открываем переданную часть файла для чтения
$in = fopen($fileChunk['tmp_name'], 'rb');
if ($in) {
// Переносим данные из части в общий файл
stream_copy_to_stream($in, $out);
fclose($in);
}
fclose($out);
} else {
exit(json_encode(['status' => 'error', 'message' => 'Не удалось открыть временный файл']));
}
// Если это последняя часть, переименовываем файл в итоговый
if ($chunkIndex === $totalChunks - 1) {
$finalFilePath = $uploadDir . $originalFilename;
rename($tempFilePath, $finalFilePath);
echo json_encode(['status' => 'success', 'message' => 'Файл успешно собран', 'path' => $finalFilePath]);
} else {
echo json_encode(['status' => 'uploading', 'chunk' => $chunkIndex]);
}
}
?>