Excel generation fails at the end

Topics: User Forum
Dec 11, 2008 at 8:13 AM
Hello,

I am trying to generate an Excel 5 document, on my Windows development machine works as expected but on the Linux production machine fails when trying to write the excel file. The code where it fails is when sending the file to the browser, I tried 2 ways and both fail:

1) Using php://output:

        $writer = PHPExcel_IOFactory::createWriter($obj, 'Excel5');
        $writer->save("php://output");

Here I get an error like ""Uncaught exception: Can't create temporary in ... OLE_File.php line 93. Here at this line the function tmpnam() fails.

2) Using a custom temporary file path:
        $tmpfile = tempnam(sys_get_temp_dir(), "hunting_phpexcel_");
        $writer = PHPExcel_IOFactory::createWriter($obj, 'Excel5');
        $writer->save($tmpfile);
        readfile($tmpfile);

This one fails without error, the file is simply empty.

I should mention that both code snippets work on Windows.

Thanks you.

Developer
Dec 11, 2008 at 9:09 AM
Could this be a permission problem on your systems temporary file directory? What do you get when you run the following?

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

var_dump(sys_get_temp_dir());
var_dump(tempnam(sys_get_temp_dir(), 'test'));
?>
Dec 11, 2008 at 1:19 PM
Hi koyama,

The files are created in /tmp folder and the dump is: string(4) "/tmp" string(15) "/tmp/testVO2waE"

No idea why it doesnt work in phpexcel...

Thanks!
Developer
Dec 11, 2008 at 3:50 PM
Ok, what do you get for this?

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$filename = tempnam('/tmp', 'test');
var_dump($filename);

var_dump(fopen($filename, "w+b"));
?>

Dec 11, 2008 at 3:58 PM
Edited Dec 11, 2008 at 3:59 PM
It returns: string(15) "/tmp/testeI15Qv" resource(3) of type (stream)

Anyhow I modified a bit the library source code to always use the system temporary folder and if I specifically say what temporary file to use it seems to work. But now another problem appeared, the generated file does not open Excel 2003 says that it has errors and tries to fix them. On Windows I dont have any errors. I compared the Linux generated file with the one on Windows and indeed there are a few bytes different. What could be wrong?

Thanks for your help!
Developer
Dec 11, 2008 at 4:21 PM
Edited Dec 12, 2008 at 4:19 PM
Good to hear that you resolved your /tmp folder problem. Not sure what the error was though.

Your new problem sounds strange. Can you send me both files to erik at phpexcel dot net ? Will have to compare them in HEX editor and see where the difference occurs although I have no clue what the problem is. Please put the files in a folder and zip it before attaching in the email. Otherwise your email client may slightly alter the Excel file (at least Outlook does).
Developer
Dec 11, 2008 at 6:08 PM
Thanks, I received the files. I think I now see what the problem is. It may sound strange, but it is likely that it is your Windows machine that has a problem and not your Linux machine.

1.
The problem seem to be that you are supplying string values using ISO 8859-1. You need to always use UTF-8 in PHPExcel. If you correct this, it should be working on your Linux machine.

2.
If you fix 1. then you will suddenly find that it is no longer working on your Windows machine. So why does it appear to work on your Windows machine? It looks like mbsting extension is not enabled on this machine. It needs to be enabled for PHPExcel_Writer_Excel5 to work correctly with special characters like ä. If  you get mbstring extension enabled it should work on your Windows machine too. It was just sort of pure luck that your Excel file came out right on your Windows machine because two problems were canceling each other.
Dec 12, 2008 at 8:23 AM
Erik you were right! It works perfectly now! I would have never find the real reason thanks a lot for your help!