How to read excel file in Codeigniter 4

I have a problem in displaying or getting the data from Excel File. Every time i run the route it gives me the error of “Failed to load resource: the server responded with a status of 500 (Internal Server Error)Understand this error”

As the output I must get the data from Excel file

can anyone help me?

Here is my controller

public function excelShow(){

    $filePath = WRITEPATH . 'public/Book1.xlsx';

    $spreadsheet = IOFactory::load($filePath);
    $worksheet = $spreadsheet->getActiveSheet();
    $rows = [];

    foreach ($worksheet->getRowIterator() as $row) {
        $cellIterator = $row->getCellIterator();
        $cellIterator->setIterateOnlyExistingCells(false);

        $cells = [];
        foreach ($cellIterator as $cell) {
            $cells[] = $cell->getValue();
        }
        $rows[] = $cells;
    }

    echo "<pre>";
    print_r($rows);
    echo "</pre>";

}

Common Issues and Fixes

1. File not found or inaccessible

$filePath = WRITEPATH . 'public/Book1.xlsx';
  • WRITEPATH in CodeIgniter points to writable/ directory.
  • If your file is in public/, then WRITEPATH . 'public/Book1.xlsx' is wrong.

Fix: Use the correct full path to your Excel file:

$filePath = FCPATH . 'Book1.xlsx'; // FCPATH points to the 'public' folder

Or if it’s in a subfolder:

$filePath = FCPATH . 'uploads/Book1.xlsx';

2. PhpSpreadsheet not installed or not imported

Make sure you’re using PhpSpreadsheet properly. Add this to the top of your controller:

use PhpOffice\PhpSpreadsheet\IOFactory;

If PhpSpreadsheet is not installed, install it with:

composer require phpoffice/phpspreadsheet

3. File format issue

Ensure your file Book1.xlsx is a valid Excel file (not corrupted or a renamed .csv or .xls).

Try opening it manually or creating a new Excel file to test.


4. PHP error reporting is off

Turn on detailed error display in CodeIgniter. In your .env file, ensure:

CI_ENVIRONMENT = development

Also, enable error display in app/Config/Boot/development.php:

error_reporting(-1);
ini_set('display_errors', 1);

5. File permission issue

Ensure PHP has permission to read the Excel file.

chmod 644 public/Book1.xlsx

Updated Controller Method

use PhpOffice\PhpSpreadsheet\IOFactory;

public function excelShow() {
    $filePath = FCPATH . 'Book1.xlsx'; // or 'uploads/Book1.xlsx'

    if (!file_exists($filePath)) {
        echo "File not found: $filePath";
        return;
    }

    try {
        $spreadsheet = IOFactory::load($filePath);
        $worksheet = $spreadsheet->getActiveSheet();
        $rows = [];

        foreach ($worksheet->getRowIterator() as $row) {
            $cellIterator = $row->getCellIterator();
            $cellIterator->setIterateOnlyExistingCells(false);

            $cells = [];
            foreach ($cellIterator as $cell) {
                $cells[] = $cell->getValue();
            }
            $rows[] = $cells;
        }

        echo "<pre>";
        print_r($rows);
        echo "</pre>";
    } catch (\Throwable $e) {
        echo "Error: " . $e->getMessage();
    }
}