[resolved] Files are unopenable when downloading

Topics: User Forum
Mar 16, 2010 at 12:26 AM

Hi, I am using PHPExcel v 1.7.2 on Vista x64 with Apache.

I can create excel 2007 files with no problem if I save them locally to the server (local file system I mean), then open that file with Excel.  However, if I try to download the file through Firefox it comes out encoded or something and Excel says it can't be opened.

I've been through so many iterations of this code I can't really remember what I've tried, but here's what I have as of right now:

 

// Create new PHPExcel object
        $objPHPExcel = new PHPExcel();
        
        $objPHPExcel->setActiveSheetIndex(0);
        $objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Hello');
        $objPHPExcel->getActiveSheet()->SetCellValue('B1', 'World');
        $objPHPExcel->getActiveSheet()->SetCellValue('A2', 'This is a ');
        $objPHPExcel->getActiveSheet()->SetCellValue('B2', 'test');
        
        // Rename sheet
        $objPHPExcel->getActiveSheet()->setTitle("test");
        
        //$filename = "" . date('H-i-s') . ".csv";
        $filename = "test " . date('y-M-d H-i-s') . ".xlsx";
        
        $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
        header("Content-Transfer-Encoding: binary");
        $objWriter->save('php://output');

 

Can anybody help me out?  I'm really tired of banging me head on this, I can't even figure out what to google for...

Thanks

Developer
Mar 16, 2010 at 12:34 AM

Can you try to run this example included in the Tests folder in the PHPExcel v 1.7.2 release :

01simple-download-xlsx.php

Mar 16, 2010 at 6:20 PM
Edited Mar 16, 2010 at 7:38 PM

Hi koyama,

I reconfigured my apache server to point to the Tests folder, but when I browse to the 01simple-download-xlsx.php file, I just get a blank page in the browser, and no download prompt.  I get nothing from any of the other test files either.

What kind of result were you hoping to get from the 01simple-download.xlsx.php file?  The code from what I can tell is virtually identical to mine (since that is where I copied it from in the first place).

 

Thanks for your help

 

EDIT:  It appears that the internet ate the previous reply that I tried to post... just jump to the next reply.

Mar 16, 2010 at 6:45 PM

After replying, I reconfigured my DocumentRoot to point at the PHPExcel folder and ran the test php files directly.  I got the exact same issue as with my test script.

"Excel cannot open the file '01simple.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."

 

As a test, I modified line 78 of the test file:

//$objWriter->save('php://output');
$objWriter->save('C:/Users/Rick/Documents/text.xlsx');

The test.xlsx file is openable directly off the hard drive with no problems.

Developer
Mar 17, 2010 at 5:47 AM

The problem is likely that there is some white space before the opening <?php tag or after the closing ?> tag. There must not be. Can you check?

Otherwise try to run 01simple-download-xlsx.php even though it looks the same as what you have.

 

Mar 17, 2010 at 2:30 PM

I did actually run the 01simple-download-xlsx.php file, I got the same error (see my previous reply).

For the record, I also checked the http headers at one point, and they were identical to the headers given by a different site (using .NET) that I can download Excel 2007 files from without difficulty.

 

Developer
Mar 19, 2010 at 1:27 AM

Try to open the corrupt file in a normal text editor or a HEX editor and compare it with the non-corrupt file you generated via $objWriter->save('C:/Users/Rick/Documents/text.xlsx');

They are supposed to be the same. The comparison may reveal some difference.

Otherwise, try to upload the corrupt file here so we can inspect:

http://phpexcel.codeplex.com/WorkItem/View.aspx?WorkItemId=10749

Mar 19, 2010 at 1:46 PM

Hello,

I have the same problem. But when I open the corrupt file with a normal text editor, there is nothing write in the file.

Mar 19, 2010 at 3:05 PM

Thanks koyama, I had the same thought last night.  I checked the files with a hex editor, and the obvious thing that jumped out was the 0D 0A at the start of the Zend file.  In my Zend controller, if I comment out the actual "save()" call, there is no output whatsoever, so I don't think Zend is echoing a newline or anything.

I've posted the Zend excel file to the location you linked above.

I also downloaded a http header addon for firefox, contents posted below.

Highlights in red:

 

http://localhost:8081/Tests/01simple-download-xlsx.php

 
GET /Tests/01simple-download-xlsx.php HTTP/1.1
Host: localhost:8081
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Referer: http://localhost:8081/Tests/

HTTP/1.1 200 OK
Date: Thu, 18 Mar 2010 17:45:41 GMT
Server: Apache/2.2.14 (Win32) PHP/5.3.2
X-Powered-By: PHP/5.3.2
Content-Disposition: attachment;filename="01simple.xlsx"
Cache-Control: max-age=0
Content-Length: 6440
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet


http://localhost:8081/report/export?reportName=MyReport

GET /report/export?reportName=JourneyAudienceReport HTTP/1.1
Host: localhost:8081
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Referer: http://localhost:8081/report?reportName=MyReport&execute=Execute
Cookie: PHPSESSID=pk5a7v5i17sq6kkea783u8hlg1

HTTP/1.1 200 OK
Date: Thu, 18 Mar 2010 18:11:10 GMT
Server: Apache/2.2.14 (Win32) PHP/5.3.2
X-Powered-By: PHP/5.3.2
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: max-age=0
Pragma: no-cache
Content-Disposition: attachment;filename="01simple.xlsx"
Content-Length: 6443
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

Mar 19, 2010 at 9:31 PM

Hi, I was reading through all the posts in the discussions regarding Zend, and I found this old post, which was starting to sound like my problem:

http://phpexcel.codeplex.com/Thread/View.aspx?ThreadId=71957

Would the encoding affect a file saved to a local hard disk on the server?  Or would it only affect files sent to php://output?

 

Mar 19, 2010 at 9:37 PM
Edited Mar 19, 2010 at 9:41 PM

RESOLVED:

 

Well I feel dumb...  My problem turned out to be that my Zend Controller WAS in fact outputting a whilespace AFTER the <?php ?> tags.  Just to reiterate, this was NOT a php_zip.dll related problem, and was NOT an actual PHPExcel issue.

 

ZEND DEVELOPERS:  PLEASE REMEMBER TO CHECK THE END OF THE FILES GENERATED IN ZEND STUDIO

 

Edit:  Also, just for the record, putting a die() or exit() inside of a controller action function does NOT stop the whitespace after the <?php ?> tags from being included in the output to the browser (in fact it comes out first!)

 

koyama, thanks for all your help!  I'm excited to actually be able to use PHPExcel now.

 

 

Mar 22, 2010 at 9:02 AM

Hello rickpelletier,

can you show me your code please? I try and I check if I have a whitespace, but I can't find them.

Maybe I don't understand how generate an excel file with zend framework.

Thanks a lot

Mar 22, 2010 at 9:32 AM

for information here is my action controller

public function exportAction(){
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);
        $objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Hello');
        $objPHPExcel->getActiveSheet()->SetCellValue('B1', 'World');
        $objPHPExcel->getActiveSheet()->SetCellValue('A2', 'This is a ');
        $objPHPExcel->getActiveSheet()->SetCellValue('B2', 'test');
        
        // Rename sheet
        $objPHPExcel->getActiveSheet()->setTitle("test");
        
        $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
        header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition:inline;filename=test.xlsx');
        $objWriter->save('php://output');
    }

 

Mar 22, 2010 at 2:06 PM

Hi onguyen,

I assume your code looks something like this:

 

<?php

//requires, etc...

class ReportController extends Zend_Controller_Action {

public function exportAction(){
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);
        $objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Hello');
        $objPHPExcel->getActiveSheet()->SetCellValue('B1', 'World');
        $objPHPExcel->getActiveSheet()->SetCellValue('A2', 'This is a ');
        $objPHPExcel->getActiveSheet()->SetCellValue('B2', 'test');
        
        // Rename sheet
        $objPHPExcel->getActiveSheet()->setTitle("test");
        
        $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
        header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition:inline;filename=test.xlsx');
        $objWriter->save('php://output');
    }

}

?>

[THIS IS WHERE WHITESPACE WAS SCREWING ME UP]

EOF

 

 

 

Apr 15, 2010 at 3:52 AM

If you have the same problem and you don't have whitespace at the end of your scritp just check there is no "echo" or any kind of output previusly to the code to download the file.

Remember: No output before the code to download the file.