Да будет код...
This commit is contained in:
38
upload.php
Normal file
38
upload.php
Normal 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]);
|
||||
}
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user