-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload.php
More file actions
60 lines (48 loc) · 2.25 KB
/
upload.php
File metadata and controls
60 lines (48 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
require 'vendor/autoload.php'; // Include the Composer autoload file for PhpSpreadsheet
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_FILES['file'])) {
$files = $_FILES['file'];
// Create a new spreadsheet for the combined data
$combinedSpreadsheet = new Spreadsheet();
$combinedSheet = $combinedSpreadsheet->getActiveSheet();
$isFirstFile = true; // Track whether it's the first file to include headers
$nextRow = 1; // Start from the first row in the combined sheet
// Loop through each uploaded file
for ($i = 0; $i < count($files['tmp_name']); $i++) {
// Skip if the file was not uploaded properly
if ($files['error'][$i] !== UPLOAD_ERR_OK) {
continue;
}
// Load the current spreadsheet
$filePath = $files['tmp_name'][$i];
$spreadsheet = IOFactory::load($filePath);
$sheet = $spreadsheet->getActiveSheet();
// If it's the first file, copy the header
if ($isFirstFile) {
$header = $sheet->rangeToArray('A1:' . $sheet->getHighestColumn() . '1');
$combinedSheet->fromArray($header, NULL, 'A1');
$isFirstFile = false; // Next files won't need the header
$nextRow++;
}
// Copy the data from the current sheet (starting from the second row) to the combined sheet
$data = $sheet->rangeToArray('A2:' . $sheet->getHighestColumn() . $sheet->getHighestRow());
$combinedSheet->fromArray($data, NULL, 'A' . $nextRow);
// Update the next row number for subsequent files
$nextRow = $combinedSheet->getHighestRow() + 1;
}
// Set the filename for the combined file
$filename = 'combined_file.xlsx';
// Output the combined file to the browser for download
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="' . $filename . '"');
header('Cache-Control: max-age=0');
$writer = IOFactory::createWriter($combinedSpreadsheet, 'Xlsx');
$writer->save('php://output');
exit();
} else {
echo "Please upload at least one file.";
}
?>