In order to differentiate from regular .zip files, the mimetype looks for files added to the .xlsx in order (as per Office 2007+), in particular:
[Content_Types].xml needs to be first followed by at least one file in the xl/ folder...
PS. It may be more prudent to move the exception for "No worksheets defined" BEFORE the file_exists/unlink check as no worksheets will result in the file being deleted without creating any output!
<Maybe even just before the $zip->close() in case any other problems/exceptions cause the .zip not to be created?>
In order to differentiate from regular .zip files, the mimetype looks for files added to the .xlsx in order (as per Office 2007+), in particular: [Content_Types].xml needs to be first followed by at least one file in the xl/ folder...
reference: https://stackoverflow.com/questions/7274030/detect-excel-xlsx-file-mimetype-via-php
Can you tweak the saveToFile function?
PS. It may be more prudent to move the exception for "No worksheets defined" BEFORE the file_exists/unlink check as no worksheets will result in the file being deleted without creating any output! <Maybe even just before the $zip->close() in case any other problems/exceptions cause the .zip not to be created?>