PHP not detecting XLS file as zip - php

I am using a library to read XLS files which internally uses PHP's zip_open() function. When creating the files locally and then uploading to my test server everything works fine. However, when I use the XLS files downloaded from a website (normal download via browser), it does not work, instead returning Error 19 meaning that the file is not seen as a zip file, which is incorrect. Excel opens the file without problems. If I re-save the file locally as an XLSX file and then upload it, I get the same error (in this instance the file is opened by the PHP's ZipArchive class). Any ideas what the reason could be? I checked that the files are not read only, possibly some Unix permissions could be set that are not displayed in Windows? (Doubt this, as the error code indicates that the file could be accessed, but could not be identified as XLS)
Using:
Apache under Windows (WAMP)
PHP 5.4.12

It seems I had misread a line of code, the zip check is only done to determine if the XLS file is an incorrectly named XLSX file. The problem with the XLS file is that it returns no sheets when parsing, I need to look into this still. I do not know why saving the XLS file as an XLSX file (using Excel) results in an incorrect ZIP archive though, but guessing it is related.

Related

PHPExcel Writer .xls file generates Excel error ... how can I change the extension to .csv?

I'm using a Wordpress plugin, wpdatatables, that uses PHPExcel to produce pdf, csv, and Excel file exports.
Anyway, when downloading an .xls file from my website, when I open it .. Excel gives me the error “The file format and extension of “blahblah.xls” don’t match. The file could be corrupted or unsafe. Unless you trust its source, don’t open it.”
Of course the file still opens fine, but I'd like to get rid of this error. One thing I noticed is the .CSV export is seemingly identical in all ways except the file extension, and opens without an error.
As someone who is not terribly familiar with PHP sadly, what direction should I look in to make PHPExcel produce .csv files only? Is there a specific function or directory in PHPExcel responsible for the Excel writer file output?

ZendService\LiveDocx returns zip file instead of docx

I'm using the ZendService\LiveDocx library, but when I run this on our stage server (Linux) and request the format to be docx, it returns me back a zip file rather than the actual document. The zip file consists of XML files describing the document. If I request the format to be PDF it works fine. This works fine in my local development environment (Windows 7) when I try to generate a docx document.
Any ideas why the LiveDocx service would return a zip file instead of the actual document?
The zip file consists of XML files describing the document.
That's how .docx files work.
A .docx in fact is a zip file, so you can simply rename them according to your needs.
You can try this by taking a "good looking" docx file and rename it to .zip, then extract.
The solution for your problem is to rename the file from .zip to .docx before exposing to the user as a download.

issue with PHP spreadsheet reader

I am facing one issue with PHP spreadsheet reader https://github.com/nuovo/spreadsheet-reader , it is reading all formats file except .xlsx , in case of .xlsx script was not running
require($docroot.'/inc/filereader/excel_reader2.php');
require($docroot.'/inc/SpreadsheetReader.inc.php');
$Spreadsheet = new SpreadsheetReader("filepath");
When i am executing php script at my localhost, SpreadsheetReader reading .xlsx file and returning desired result, but when i am trying to run same code on some server then it is only executing for(.csv,.xls,.txt) but not for .xlsx.
I have already checked folder and file permissions for above issue at server.
What may be problem here?
An xlsx file is actually a zip file containing the spreadsheet payload as a set of xml files. This means that you need a php instance with the modules for zip and xml support loaded. The easiest way to check whether the correct modules are loaded is by executing phpinfo, like this
<?php
phpinfo();
?>
If the correct modules are not loaded you will probably have to contact your webhoster.
actually date_default_timezone_set('Asia/Calcutta'); is not set in spreadsheet reader and it is throwing exception

PHP: Zipping files as xlsx causes file to become invalid

I have to replace variables inside a user-submitted xlsx file and am doing it this way:
Rename the .xlsx to .zip
Unzip to a temp-folder
Make the necessary changes
Zip the files
Rename the .zip to .xslx
I am using plain ZipArchive in PHP. When I try to open the generated .xlsx in Excel, it fails with a message format or extension invalid. When I compress the temporary files with WinRAR (as zip), and rename the resulting file to .xlsx, it works. The zip files generated with both methods contain the same data structure and files, but the WinRAR file is slightly bigger (10.2K vs. 10.3K with normal compression).
When viewing the garbled code of the files, I could see that the files appear in a different order, but don't know if that's the cause. Any clue would be greatly appreciated.
I got it to work with another component, PclZip (phpconcept.net/pclzip). It is a class that apparently uses gzip, and renaming it's outputs to .xlsx are fine with Excel 2007.

Serving files from SQL Server through Zend

I am storing files into an image field in SQL server storing the string in hex after converting into using:
unpack("H*hex", $datastring);
If I read from the database and write to a file, then open the file locally I can open it up just fine. But if I try to send the file as an attachment to the browser the file becomes corrupted and unreadable. Currently, the files in question are PDF and MSWord documents.
I am setting content-type to the appropriate MIME type for the files and setting content-disposition to attachment. I've tried various ways of streaming the file including dumping the string directly from the database and writing first to a file then reading the file (either line by line or with readfile()).
I am using a slightly customized version of the Zend framework, but I'm not sure if that is causing any issues. What should I do to send files to the browser?
How do you serve them?, in theory if you are using MVC, you need to disable the view at the end of your controller to avoid extra content being inyected at the bottom of your file.

Categories