I have a simple PHP app with functionality for downloading an Excel file. On my local machine, using XAMPP, users can download the Excel sheet without issues. However, after deploying the PHP app to an Azure Web App environment, the Excel download function no longer works
I am using xampp with php version: PHP Version 8.2.1
2 and on the azure environment I am using: Major version 8
and Minor version:8
This is the output if a user tries to download an excel file:
PKZJ^YG�D�Z�[Content_Types].xmlSV���N�0E�|E�-J��@5��*Q>`�'�UǶl���L����@�nbE�gr=��tW�d�>(k 6�r��V*�,���)�cI�`$hk�`{l:�/�CBb V���9��Bf�RZ_C�W��� ��o���k"���ƃM�/��Jb2��&�i� ��(#?�X?c���k�*�(_�����}�>���k�PKZJ^Y�78�K_rels/.relsSV���j�0��{ �{���1F�^ʠ�2��l�$���-}
And with devtools I don’t see any errors.
I checked in
azure Advanced Tools –> Go –> index of WWWROOT.
And I see the vendor folder in it with also the composer.json in it. So the libraries are available.
And this is the function for download the excel file:
<?php
require 'vendor/autoload.php';
use PhpOfficePhpSpreadsheetSpreadsheet;
use PhpOfficePhpSpreadsheetWriterXlsx;
function exportKvKNumbersToExcel(array $kvkNumbers, $dateInput)
{
ob_start();
ob_clean();
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setTitle('KvK Numbers');
$sheet->setCellValue('A1', 'KvK Number');
$row = 2;
foreach ($kvkNumbers as $kvkNumber) {
$sheet->setCellValue('A' . $row, $kvkNumber);
$row++;
}
$writer = new Xlsx($spreadsheet);
$fileName = 'KvK_Numbers_'. $dateInput .'_' . date('Y-m-d_H-i-s') . '.xlsx';
ob_end_clean();
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename="' . $fileName . '"');
header('Cache-Control: max-age=0');
$writer->save('php://output');
exit;
}
Do I have to set some settings in the azure environment?
Question: How to download an excel file from the azure environment?