Redirecting To Browser Creates Corrupted File

Topics: Developer Forum, Project Management Forum, User Forum
Aug 7, 2009 at 4:25 PM

I have PHP v5.3.0 installed on my Linux server.  The settings are:

Configure Command

'./configure' '--build=i686-pc-linux-gnu' '--host=i686-pc-linux-gnu' '--target=i386-redhat-linux-gnu' '--program-prefix=' '--prefix=/usr' '--exec-prefix=/usr' '--bindir=/usr/bin' '--sbindir=/usr/sbin' '--sysconfdir=/etc' '--datadir=/usr/share' '--includedir=/usr/include' '--libdir=/usr/lib' '--libexecdir=/usr/libexec' '--localstatedir=/var' '--sharedstatedir=/usr/com' '--mandir=/usr/share/man' '--infodir=/usr/share/info' '--cache-file=../config.cache' '--with-libdir=lib' '--with-config-file-path=/etc' '--with-config-file-scan-dir=/etc/php.d' '--disable-debug' '--with-pic' '--disable-rpath' '--without-pear' '--with-bz2' '--with-curl' '--with-exec-dir=/usr/bin' '--with-freetype-dir=/usr' '--with-png-dir=/usr' '--enable-gd-native-ttf' '--without-gdbm' '--with-gettext' '--with-gmp' '--with-iconv' '--with-jpeg-dir=/usr' '--with-openssl' '--with-png' '--with-pspell' '--with-expat-dir=/usr' '--with-pcre-regex=/usr' '--with-zlib' '--with-layout=GNU' '--enable-exif' '--enable-ftp' '--enable-magic-quotes' '--enable-sockets' '--enable-sysvsem' '--enable-sysvshm' '--enable-sysvmsg' '--enable-track-vars' '--enable-trans-sid' '--enable-yp' '--enable-wddx' '--with-kerberos' '--with-unixODBC=shared,/usr' '--enable-memory-limit' '--enable-shmop' '--enable-calendar' '--enable-dbx' '--enable-dio' '--with-mime-magic=/usr/share/file/magic.mime' '--without-sqlite' '--with-libxml-dir=/usr' '--with-xml' '--enable-force-cgi-redirect' '--enable-pcntl' '--with-imap=shared' '--with-imap-ssl' '--enable-mbstring=shared' '--enable-mbstr-enc-trans' '--enable-mbregex' '--with-ncurses=shared' '--with-gd' '--enable-bcmath=shared' '--enable-dba=shared' '--with-db4=/usr' '--with-xmlrpc=shared' '--with-ldap=shared' '--with-mysql=shared,/usr' '--with-mysqli=shared,/usr/bin/mysql_config' '--enable-dom' '--with-dom-xslt=/usr' '--with-dom-exslt=/usr' '--with-pgsql=shared' '--enable-soap=shared' '--with-xsl=shared,/usr' '--enable-xmlreader' '--enable-xmlwriter' '--enable-fastcgi' '--enable-pdo=shared' '--with-pdo-odbc=shared,unixODBC,/usr' '--with-pdo-mysql=shared,/usr' '--with-pdo-pgsql=shared,/usr' '--with-pdo-sqlite=shared,/usr' '--with-readline' '--with-tidy' '--with-apxs2=/usr/sbin/apxs' '--enable-zip'

Additional .ini files parsed

/etc/php.d/bcmath.ini, /etc/php.d/dba.ini, /etc/php.d/dom.ini, /etc/php.d/gd.ini, /etc/php.d/imap.ini, /etc/php.d/ldap.ini, /etc/php.d/mbstring.ini, /etc/php.d/mysqli.ini, /etc/php.d/mysql.ini, /etc/php.d/ncurses.ini, /etc/php.d/odbc.ini, /etc/php.d/pdo.ini, /etc/php.d/pdo_mysql.ini, /etc/php.d/pdo_odbc.ini, /etc/php.d/pdo_pgsql.ini, /etc/php.d/pdo_sqlite.ini, /etc/php.d/pgsql.ini, /etc/php.d/soap.ini, /etc/php.d/xmlreader.ini, /etc/php.d/xmlrpc.ini, /etc/php.d/xmlwriter.ini, /etc/php.d/xsl.ini

Registered PHP Streams

https, ftps, compress.zlib, compress.bzip2, php, file, glob, data, http, ftp, phar, zip

Registered Stream Socket Transports

tcp, udp, unix, udg, ssl, sslv3, sslv2, tls

Registered Stream Filters

zlib.*, bzip2.*, convert.iconv.*, string.rot13, string.toupper, string.tolower, string.strip_tags, convert.*, consumed, dechunk

 


 

When I run this script:

<?
/** Error reporting */
error_reporting(E_ALL);

/** Include path **/
ini_set('include_path',ini_get('include_path').PATH_SEPARATOR.'../private/utilities/PHPExcel/Classes/');

/** PHPExcel */
include 'PHPExcel.php';

/** PHPExcel_Writer_Excel2007 */
include 'PHPExcel/Writer/Excel2007.php';

// Create new PHPExcel object
//echo date('H:i:s')." Create new PHPExcel object<br>";
$objPHPExcel = new PHPExcel();

// Set properties
//echo date('H:i:s')." Set properties<br>";
$objPHPExcel->getProperties()->setCreator('Maarten Balliauw');
$objPHPExcel->getProperties()->setLastModifiedBy('Maarten Balliauw');
$objPHPExcel->getProperties()->setTitle('Office 2007 XLSX Test Document');
$objPHPExcel->getProperties()->setSubject('Office 2007 XLSX Test Document');
$objPHPExcel->getProperties()->setDescription('Test document for Office 2007 XLSX, generated using PHP classes.');

// Add some data
//echo date('H:i:s')." Add some data<br>";
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('A1','Hello');
$objPHPExcel->getActiveSheet()->SetCellValue('B2','world!');
$objPHPExcel->getActiveSheet()->SetCellValue('C1','Hello');
$objPHPExcel->getActiveSheet()->SetCellValue('D2','world!');

// Rename sheet
//echo date('H:i:s')." Rename sheet<br>";
$objPHPExcel->getActiveSheet()->setTitle('Simple');

// redirect output to client browser
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="myfile.xlsx"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');
$objWriter->save('php://output');

// Echo done
//echo date('H:i:s').' Done writing file.';
?>

It get an .xlsx file that gives this error when I try to open it in Excel 2007:

Excel cannot open the file 'myfile.xlsx' because the file format or file extension is not valid.  Verify that the file has not been corrupted and that the file extension matches the format of the file.


First, let me say that for some reason almost all of the php scripts in the PHPExcel v1.6.7 release were missing the closing '?>' tag.  I added the closing tag to all of the scripts that were missing it.  Even after doing that, I still have this problem with the corrupted file.  The unaltered 'Hello World' script works.  The file is saved on my server and, after downloading it to my local machine, it opens without error using Excel 2007.  My only guess is that maybe one or more files have the BOM mentioned in 4.4.32.  How can I check for this?  Other than that, what might cause the redirected file to be corrupted?

Thank you.

Developer
Aug 7, 2009 at 5:31 PM

>> It get an .xlsx file that gives this error when I try to open it in Excel 2007

Is this only happening when the file is redirected to the client browser, or also when you save the Excel file normally to the disk on the server. I recommend that you first try to save the file normally on disk and get that working.

>> First, let me say that for some reason almost all of the php scripts
>> in the PHPExcel v1.6.7 release were missing the closing '?>' tag.

This is on purpose. The advantage leaving out ?> is that you don't have to worry about whitespace making trouble after the ?>
http://php.net/basic-syntax.instruction-separation

>> My only guess is that maybe one or more files have the BOM mentioned in 4.4.32.
>> How can I check for this?

Open the downloaded Excel file in a HEX editor and see if file starts with EF BB BF (). But since you have not included other files in your script I don't think this is the problem. None of the PHPExcel class files have BOM.

>> Other than that, what might cause the redirected file to be corrupted?

Sometimes, it is because PHP memory has been exhausted. The file then just contains a PHP error message in plain text. Try to open the Excel file in a text editor and check.

Aug 7, 2009 at 10:07 PM

As I mentioned, the unaltered 'Hello World' script works.  The file is saved on my server and, after downloading it to my local machine, it opens without error using Excel 2007.  I opened the download Excel file with a HEX editor and then with Notepad.  You are right, it contained the error message below:

Fatal error: Class 'PHPExcel_IOFactory' not found in /home/virtual/site1/fst/var/www/html/excel.php on line 55

I included the file and it works.  Thanks for your help.