0

I want to import excel files to read its content but it shows me an error that says "500 Internal Server Error". My PHP version that I'm currently using is PHP Version 7.4.29. Is there a problem with the version I'm using in XAMPP or Spreadsheet?

My PHP sourcecode for PHPSpreadsheet to read excel:

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\IOFactory;

public function readExcelSpreadsheet() {
    require '../PHPSpreadsheet/vendor/autoload.php';
    $arr = [];
    $arr['items'] = [];
    $fileName = $_FILES['importExcelSpreadsheet']['name'];
    $importPath = "../PHPSpreadsheet/import/";
    
    if (
        move_uploaded_file($_FILES['importExcelSpreadsheet']['tmp_name'], 
        "{$importPath}{$fileName}")
        ) {
      if (isset($fileName)) {
        $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReaderForFile("{$importPath}{$fileName}");
        $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load("{$importPath}{$fileName}");
        $data = $spreadsheet->getActiveSheet()->toArray();
  
        foreach ($data as $row) {
          $items = [
            'name'  => $row['0'],
            'email' => $row['1']
          ];
          array_push($arr['items'], $items);
        }      
        return $arr;
      }
    }
  }

Here's my AJAX request:

$(document).on("change", "#primary-btn-import-for-import-csv", function () {
  if ($("#primary-btn-import-for-import-csv")[0].files.length != 0) {
    $("#primary-email-lists-for-import-csv").multiselect("rebuild");

    let form = new FormData();
    form.append("importExcelSpreadsheet", $("#primary-btn-import-for-import-csv")[0].files[0]);

    $.ajax({
      url: baseUrlAction() + "?btn=readExcelSpreadsheet",
      type: "POST",
      dataType: "JSON",
      data: form,
      contentType: false,
      processData: false,
      cache: false,
      error: function (error) {
        console.log(error.responseText);
      },
      success: function (response) {
        let options;
        for (let i = 0; i < response.length; i++) {
          options += 
            '<option value="' + response[i].name + ";" + response[i].email + '">' +
              response[i].email +
            "</option>";
        }
        $("#primary-email-lists-for-import-csv").html(options);
        $("#primary-email-lists-for-import-csv").multiselect("rebuild");
        $("#primary-email-lists-for-import-csv").multiselect("refresh");
      },
    });
  }
});
  • 500 Internal Server Error is a generic error message informing you that the server crashed while processing the request. Beyond that, it's (intentionally) meaningless, and is of very little use for debugging. You need to check the error logs on the server to try and find the underlying exception message. Once you've got that, you stand a chance of identifying the problem. See also [How can I get useful error messages in PHP?](https://stackoverflow.com/questions/845021/how-can-i-get-useful-error-messages-in-php) – ADyson Sep 23 '22 at 09:35
  • 1
    Its almost definitely some of your code, in some situations you just have to chop the code down to its bare bones to get the page to run, then add back line(s) at a time to see when you get the crash again, then you know which line of code is causing the crash and you can start debuggung it – RiggsFolly Sep 23 '22 at 09:44
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 23 '22 at 10:59
  • My specific problem is that I receive a 500 Internal server error whenever I import a csv file to read, but when I use the latest version of XAMPP, it works. – Jhay Lawrence Sep 24 '22 at 00:55

0 Answers0