PHPExcel in CakePHP: format/file extension not valid.

Topics: User Forum
Feb 9, 2011 at 9:34 AM
Edited Feb 9, 2011 at 9:36 AM

Hi I've been following this guide:

http://bakery.cakephp.org/articles/melgior/2010/01/26/simple-excel-spreadsheet-helper#comment4d5247cb-b344-4b55-bde4-281eadcbdab8

and after all is done, as I trigger the action and supposing to download/open an excel file, I managed to do both except that the file opened is empty with the error box:

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

I'm not sure if this problem was similar: http://phpexcel.codeplex.com/Thread/View.aspx?ThreadId=53274

But it sounds like the same problem, the last one. Not sure though, but does anyone here care to share what's going on?

 

Coordinator
Feb 9, 2011 at 10:00 AM
Edited Feb 9, 2011 at 10:00 AM

If you open the file in a text editor, what are the first few characters (including any white space characters such as tabs, new lines, etc?

Feb 9, 2011 at 11:44 AM
MarkBaker wrote:

If you open the file in a text editor, what are the first few characters (including any white space characters such as tabs, new lines, etc?

Hi Mark,

Here's what I find in my first line when I open the downloaded file:

1. <pre class="cake-debug"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr1-trace').style.display = (document.getElementById('cakeErr1-trace').style.display == 'none' ? '' : 'none');"><b>Warning</b> (2)</a>: Invalid argument supplied for foreach() [<b>APP\views\helpers\excel.php</b>, line <b>35</b>]<div id="cakeErr1-trace" class="cake-stack-trace" style="display: none;"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr1-code').style.display = (document.getElementById('cakeErr1-code').style.display == 'none' ? '' : 'none')">Code</a> | <a href="javascript:void(0);" onclick="document.getElementById('cakeErr1-context').style.display = (document.getElementById('cakeErr1-context').style.display == 'none' ? '' : 'none')">Context</a><div id="cakeErr1-code" class="cake-code-dump" style="display: none;"><pre><code><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;_headers()&nbsp;{
2. </span></code>
3. <code><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$i=0;
4. </span></code>


It's the first 4 lines I copied from the file in my text editor. It's in .xlsx extension. It looks fine from the appearance.

Feb 9, 2011 at 12:18 PM

how do you mean it looks fine?  Apparantly cake is throwing a warning about a foreach! ;) My hunch would be that you are trying to foreach over a non array. Although lowering your error_Reporting may solve the symptoms, I would take a closer look at the foreach if I were you ;)

Coordinator
Feb 9, 2011 at 12:34 PM
Edited Feb 9, 2011 at 12:39 PM
@JohnMaxim
The first few characters of an xlsx file should look something like
PK     ! � j��  �   �[Content_Types].xml ��(  

But as borft says, this is cake displaying a warning before the actual xlsx output... by an attempt to iterate over a non-array at line 35 of APP\views\helpers\excel.php

I don't use cake, so I can't tell you how easy it is to debug... but the backtrace suggests where the error is being generated.

Warning (2): Invalid argument supplied for foreach()
[APP\views\helpers\excel.php, line 35]

Feb 9, 2011 at 1:25 PM
Edited Feb 9, 2011 at 1:30 PM

 

<pre class="cake-debug"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr1-trace').style.display = (document.getElementById('cakeErr1-trace').style.display == 'none' ? '' : 'none');"><b>Notice</b> (8)</a>: Undefined offset: 0 [<b>APP\views\helpers\excel.php</b>, line <b>35</b>]<div id="cakeErr1-trace" class="cake-stack-trace" style="display: none;"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr1-code').style.display = (document.getElementById('cakeErr1-code').style.display == 'none' ? '' : 'none')">Code</a> | <a href="javascript:void(0);" onclick="document.getElementById('cakeErr1-context').style.display = (document.getElementById('cakeErr1-context').style.display == 'none' ? '' : 'none')">Context</a><div id="cakeErr1-code" class="cake-code-dump" style="display: none;"><pre><code><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;_headers()&nbsp;{
</span></code>
<code><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$i=0;
</span></code>

Ok, so I changed the display to only one item which is using findById($id), (just to test where went wrong).
But I'm still getting the same error from excel except now the content in the file has changed as pasted above.
I simply fix the id value a number = '1' just for testing.

Here's my cake's debug code:
Array
(
    [0] => Array
        (
            [User] => Array
                (
                    [id] => 1
 [username] => JohnMaxim
 [password] => ...
................
................
Originally, I'm trying to get all the usernames printed on excel. The codes are here:(after modifying so only 1 username can be printed)

function member($id = '1') {
        $this->layout = 'ajax';
        $this->set('members', $this->User->findById($id));
    }


and display page:

<?php $excel->generate($members['User'], 'Members list '.$members['User']['username']); ?>

I'm not sure where is wrong. Is it still in array now? But I tested with only 1 (result) username that is JohnMaxim but the excel file doesn't open, still showing the
same error. I supposed there's a username: JohnMaxim in the excel file?

But it's still either corrupted or wrong extension. If someone knows Cake, can you show me how to output the usernames properly, e.g. for a list of usernames? since
the example shown from the link is only what I currently know. I tried
<?php foreach ($members as $member): ?>   
<?php $excel->generate($member['User'], 'Members list '.$member['User']['username']); ?>
<?php endforeach; ?>

 

 

It doesn't work. Only 1 username is printed as the excel's filename but it can't be opened, prompting the same error as invalid extension or corrupted.

 

Feb 9, 2011 at 4:54 PM

You still haven't solved the initial problem ;) first you tried to iterate over a variable that wasn't iterable (meaning it wasn't an array or object). Now you've just changed the code to only use the first element of a variable that still isn't an array ;)

 

I suggest you try vardumping or print_ring the variable that is causing the notice.

Feb 10, 2011 at 3:21 AM

sorry but what are you talking about?? the current output I have for testing isn't supposed to be an array. Why are you asking me to make an array ?? I only require to test if a single output is to display,

whether the excel can be opened. And it still can't be opened although 1 output is to be printed. About the vardumping do you mean print debug?? if so, I have already shown above.

Feb 10, 2011 at 7:44 AM

If you look at the error message, it tells you that you are trying to refer to a non existing element of an array:

<b>Notice</b> (8)</a>: Undefined offset: 0 [<b>APP\views\helpers\excel.php</b>, line <b>35</b>

I'm guessing (but I might be wrong ofcourse), that you changed your foreach to only processing the first element by something like the following:

foreach ( $array as $element ){ doSomething($element); }

to

doSomething($array[0]);

But because $array is not an array, php and cake are still throwing an error.
Feb 28, 2013 at 4:59 PM
For those who wonders, setting your debug level to 0 in core.php will take away this "offset" error and render a valid xls file.