PHPExcel - error while trying to insert image after loading and writing
append, image, php, phpexcel
Solution
I think this is a bug in PHPExcel in `Classes\PHPExcel\Writer\Excel2007.php`. Its easy to fix.
Short answer is: comment out or remove lines 235-237. That would be this code:
if (file_exists($pFilename)) {
unlink($pFilename);
}
Your code will work then. I've checked, it works for me now.
Now, some longer explanation. On lines 235-243 there is this code:
if (file_exists($pFilename)) {
unlink($pFilename);
}
// Try opening the ZIP file
if ($objZip->open($pFilename, $zipOverWrite) !== true) {
if ($objZip->open($pFilename, $zipCreate) !== true) {
throw new PHPExcel_Writer_Exception("Could not open " . $pFilename . " for writing.");
}
}
Now, in this code, three things take place in order:
- If we're saving data to existing file, then this file is deleted (`unlink`)
- If we're saving data to existing file, overwrite this file
- If we're saving data to non-existing file, create it
As you see, operation from step 2 is never executed because in step 1 file is always deleted. And in this process all previously existing file attachments are lost. That's why later you get error `File XXX does not exist` - indeed, your earlier image files are not present in newly created file.
Obvious fix to this problem is to remove step 1. If file exists, it should be overwritten. I do not see any logical explanation for step 1. Maybe its a left over from some old code.
Problem
i saw this problem in so many posts. but none have been answered. first time when I insert image in an excel file, no problem is there.but if i load that excel file again and try to insert another image in another cell,following problem occurs: ``` Fatal error: Uncaught exception 'PHPExcel_Writer_Exception' with message 'File zip://C:\xampp\htdocs\Well\test.xlsx#xl/media/well1.bmp does not exist' in C:\xampp\htdocs\Well\Classes\PHPExcel\Writer\Excel2007\ContentTypes.php:242 Stack trace: #0 C:\xampp\htdocs\Well\Classes\PHPExcel\Writer\Excel2007\ContentTypes.php(181): PHPExcel_Writer_Excel2007_ContentTypes->_getImageMimeType('zip://C:\xampp\...') #1 C:\xampp\htdocs\Well\Classes\PHPExcel\Writer\Excel2007.php(246): PHPExcel_Writer_Excel2007_ContentTypes->writeContentTypes(Object(PHPExcel), false) #2 C:\xampp\htdocs\Well\test.php(125): PHPExcel_Writer_Excel2007->save('test.xlsx') #3 {main} thrown in C:\xampp\htdocs\Well\Classes\PHPExcel\Writer\Excel2007\ContentTypes.php on line 242. ``` This is my code to draw: ``` `$objDrawing = new PHPExcel_Worksheet_Drawing(); $objDrawing->setName('image'); $objDrawing->setDescription('nnnnn'); $objDrawing->setPath('images/well.bmp'); $objDrawing->setCoordinates('I'.$s); $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());` ``` I searched lot and I saw some posts regarding it. but none of them have been answered. Edit: My code: ``` require_once 'Classes/PHPExcel.php'; require_once 'Classes/PHPExcel/IOFactory.php'; $name='statistics.xlsx'; if(file_exists($name)) { $objPHPExcel = PHPExcel_IOFactory::load($name); $lastRow = $objPHPExcel->getActiveSheet()->getHighestRow(); $j=$lastRow+10; } else { $j=1; $objPHPExcel = new PHPExcel(); } $i='A'; $objPHPExcel->setActiveSheetIndex(0); // I am not writing the entire data $objPHPExcel->getActiveSheet()->setCellValue($i++.$j, 'name'); $objDrawing = new PHPExcel_Worksheet_Drawing(); $objDrawing->setName('image'); $objDrawing->setDescription('image'); $objDrawing->setPath('image.png'); $objDrawing->setCoordinates('E'.$s); $objDrawing->setWorksheet($objPHPExcel->getActiveSheet()); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter->save($name); ```