action_revisar_excel.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. #display PHP errors
  3. $ruta = "../";
  4. require_once "../include/bd_pdo.php";
  5. require_once "../include/func_excel.php";
  6. require_once "../include/func_string.php";
  7. use PhpOffice\PhpSpreadsheet\IOFactory;
  8. $reader = IOFactory::createReader("Xlsx");
  9. $reader->setReadDataOnly(true);
  10. $file = $_FILES['archivo'];
  11. $spreadsheet = $reader->load($file['tmp_name'][0]);
  12. $data = [];
  13. try {
  14. foreach_sheet(
  15. $spreadsheet, // object $spreadsheet
  16. function (array $row_data, int $i, string $sheet) {
  17. global $horario, $data;
  18. if (renglón_vacío($row_data)) return;
  19. validar_registro($row_data, $i);
  20. $horario["horario"] = horario($row_data, $i, $sheet);
  21. foreach (array_filter($row_data) as $key => $value)
  22. $horario = array_merge($horario, ($key == 'maestro') ? columna_nombre($value) : [$key => $value]);
  23. $data[] = $horario;
  24. }
  25. );
  26. die(json_encode([
  27. "status" => "success",
  28. "message" => "Horario revisado con éxito, se leyeron: " . count($data) . " registros",
  29. "data" => $data
  30. ]));
  31. } catch (Exception $e) {
  32. die(json_encode([
  33. "status" => "error",
  34. "message" => $e->getMessage(),
  35. ]));
  36. }