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>";
}
It looks like you shared only a small part of the problem, so maybe more details would help.
Also, I don't know what kind of environment you're running on (Windows, Linux, local server, production?), so I'm covering a few possibilities here.
But based on what you posted, here are some things you should check:
1. Set CI_ENVIRONMENT = development
in your .env file so maybe we can see an exact error message
If display_errors = Off
is set in your php.ini
and the server does not allow it to be changed at runtime, then setting CI_ENVIRONMENT = development
alone won't be enough to show errors.
You can check it during runtime with:
echo ini_get('display_errors');
echo ini_get('error_reporting');
If display_errors returns 0, it means that error displaying is disabled on the server, and you will need to enable it manually in the server configuration.
2. Check the file path if it exists or not first
if (!file_exists($filePath)) {
die('File not found: ' . $filePath);
}
Not sure, but you probably want:
$filePath = ROOTPATH . 'public/Book1.xlsx';
3.Is the IOFactory imported on the top of the controller?
use PhpOffice\PhpSpreadsheet\IOFactory;
Otherwise, IOFactory::load()
will not work and you'll get a fatal error internally.
4. If you're on Linux or a server, it's also possible that the PHP process doesn't have permission to read the file.
Make sure the .xlsx
file has at least read permissions.
For example, you can run:
chmod 644 public/Book1.xlsx
If you can share the full error message after setting development mode, it would be even easier to find the exact issue.