0

I have this PHP code for importing excel to mysql using PhpSpreadsheet in xampp. This code is OK when running in windows environment but if I hosted it to a Linux Server(Ubuntu 22.04) using XAMPP Linux got an error "HTTP ERROR 500". I've installed all packages and extension needed for PhpSpreadsheet based here on phpspreadsheet.readthedocs.io documentation. If this error is for the server side what should I do? Please help

These are my codes for my import file

composer.json

{
"name": "mark/import1",
"autoload": {
    "psr-4": {
        "Mark\\Import1\\": "src/"
    }
},
"authors": [
    {
        "name": "mark"
    }
],

 "require": {
    "phpoffice/phpspreadsheet": "^1.28"
},
"config": {
    "platform": {
        "php": "8.1"
    }
}
}

import code

<?php
 $con = mysqli_connect('localhost','root','','db_import');

 require 'vendor/autoload.php';

 use PhpOffice\PhpSpreadsheet\Spreadsheet;
 use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

  if(isset($_POST['save_excel_data']))
  {
   $fileName = $_FILES['import_file']['name'];
   $file_ext = pathinfo($fileName, PATHINFO_EXTENSION);

   $allowed_ext = ['xls','csv','xlsx'];

  if(in_array($file_ext, $allowed_ext))
  {
    $inputFileNamePath = $_FILES['import_file']['tmp_name'];
    $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($inputFileNamePath);
    $data = $spreadsheet->getActiveSheet()->toArray();

foreach($data as $row)
{
            $fullname = $row['0'];
            $email = $row['1'];
            $phone = $row['2'];
            $course = $row['3'];
    
    $studentQuery = "INSERT INTO students (fullname,email,phone,course) 
                            VALUES('$fullname','$email','$phone','$course')";
            $result = mysqli_query($con, $studentQuery);
            $msg = true;    
}
 }
 }
8
  • Look at the slashes in your code. This is for a Windows system. For a Linux system they go the other way. $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($inputFileNamePath
    – David
    Apr 1, 2023 at 12:16
  • @David hi sir. I've already did that but still problem exist and also run this in a ubuntu desktop22.04 but no luck
    – moski
    Apr 1, 2023 at 12:28
  • You have changed ALL the wrong slashes everywhere?
    – David
    Apr 1, 2023 at 12:31
  • @David yes sir by changing to forward slash /
    – moski
    Apr 1, 2023 at 12:34
  • @Jos I dont see any apache2 from /var/log
    – moski
    Apr 1, 2023 at 13:51

0

You must log in to answer this question.

Browse other questions tagged .