0

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;
Hamed
  • 313
  • 2
  • 15
  • 1
    Not sure how embedded images work in Excel, so this may not make any sense, but: is the image one of the [file types supported by getimagesize](https://stackoverflow.com/questions/1282351/what-kind-of-file-types-does-php-getimagesize-return)? If not, can you change it so it is? – Matt Raines Jul 06 '21 at 20:29
  • @MattRaines Thank you very much. That was exactly the problem. I saved the photo as png and replaced the photo in the file and the problem was solved. – Hamed Jul 06 '21 at 20:39

0 Answers0