Sending the output directly to the browser doesn't work

Topics: User Forum
Aug 30, 2007 at 9:58 AM

I've created a sheet, that's sent as a strem directly to the browser:

header("Content-type: application/");
header("Content-disposition: attachment; filename=mySheet-" . date("d-m-Y").".xls");

$objWriter = new PHPExcelWriterExcel5($this->sheet);

When hosted locally it works perfectly, but when uploaded to my webhots the excel file only contains this line:

"Can't create temporary file"

I have a pretty good idea what the problem is (/tmp isn't writable - but it seems to be though). Anyway to debug this further? Is there anyway to change where this temporary file is saved?

Any help is appreciated greatly!
Aug 30, 2007 at 11:23 AM
No idea on that one... Perhaps you can output to file first, and then stream the file to the browser?
Sep 6, 2007 at 10:42 PM
Edited Sep 6, 2007 at 10:43 PM

Unfortunately that doesn't seem to work either. I get the same error message.

I've been looking into it, and it seems to be a problem with the script (/Excel5/Workbook.php and OLE_file.php) assuming that I have permissions to write in files created in /tmp - my webhost doesn't agree in this assumption. Yes; I'm using shared hosting :(

In Workbook.php on line 574 the init() handle is called on an instance of PHPExcelWriterExcel5OLEPPSFile (OLEfile.php) which raises the exception 'Can't create temporary file'. As $this->tmpdir is set to '' in the constructor, tempnam creates the file in /var/tmp (which is the default tmp dir on my host). The file is actually created, but fopen() fails and the exception is thrown. My webhost claims that this is the normal behavior - i.e. php (and not me) owns files created by php.

I know that there is an easy (but not exactly portable) fix for this (setting up a dir outside my webscope and hardcode it in the files mentioned above), but maybe a better solution could be found? Furthermore I not convinced, that my webhost is right, but I'm not that much into file permissions and ownership ;)

Any suggestions?
Sep 7, 2007 at 8:12 AM
Normally if user "apache" (under which PHP usually runs) can create a file in /var/tmp, it should also be able to read there...

Tempnam usually tries writing in the current directory first, that is the directory where your script runs. Try chmodding that directory to 0644.

Is safe mode enabled on your server?
Sep 7, 2007 at 10:29 AM

I this case the script doesn't just try to open the file for reading it tries to open it for writing i.e. fopen(..., 'w+b'). But it turns out, that it can't even read it (tried fopen(... 'r')).

Warning: fopen() function.fopen: SAFE MODE Restriction in effect. The script whose uid/gid is 65534/65533 is not allowed to access /var/tmp/OLEPPSFileh3igk8 owned by uid/gid 80/0 in /usr/home/customers/<domain>/wwwroot/www/analyse/classes/PHPExcel/Writer/Excel5/OLE_File.php on line 89

This clearly is a safe mode issue, and since my webhost insists that I do not have permissions to access files in /var/tmp I guess there is no other option than to set up my own tmp dir and hardcode it in OLE_File.php.

As for tempnam() I'm not sure that you are right. The docs does states, that if the first argument i empty or not a valid directory it will default to the system tmp dir. It doesn't say anything about writing the file in the working directory.
Sep 10, 2007 at 6:05 AM
True, true.
I think setting a hard-coded path should do the trick then?
Sep 11, 2007 at 11:05 AM
Edited Sep 11, 2007 at 11:08 AM

My point being. Maybe it would be an idea to add a configuration file in the distribution - most people wouldn't need to edit it, but it would make the user experience a bit more straight forward ;)

Thanks for helping me out!
Sep 12, 2007 at 5:33 AM
The OLE library actually is a wrapped PEAR class, we never thought it would be necessary to make that configurable :-)

Anyway, a work item is on its way to the next release!
Sep 12, 2007 at 5:35 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.