I want to use phpspreadsheet to read a pre-made Excel file and modify it and give it to the user to download. (PhpSpreadsheet version is 1.18 and my php version is 7.4) There is also a company logo in this pre-made file, but when I want to save the file after the changes, I get the following error:
Trying to access array offset on value of type bool
This error occurs in this line of PhpSpreadsheet package code:
# vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Writer\Xlsx\ContentTypes.php:196
private function getImageMimeType($pFile)
{
if (File::fileExists($pFile)) {
$image = getimagesize($pFile); // return false
return image_type_to_mime_type($image[2]); //Error line
}
throw new WriterException("File $pFile does not exist");
}
Checking the $pFile
existence with php file_exist()
function returns false but the package function File::fileExists()
returns true.
The value of the $pFile variable is as follows:
zip://S:\server\www\project\web\uploads_n\sheet.xlsx#xl/media/image1.emf
And if I delete this image from excel file, everything is ok!
This code is also where I try to modify the pre-made Excel file:
$objPHPExcel = IOFactory::load('uploads_n\sheet.xlsx');
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue('A' . 5, "project1");
$objPHPExcel->getActiveSheet()->setCellValue('B' . 5, "done");
$filename = 'projects_report.xlsx';
$objWriter = IOFactory::createWriter($objPHPExcel, "Xlsx");
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition", 'attachment;filename="' . $filename . '"');
header("Cache-Control: max-age=0");
$objWriter->save("php://output");
exit;