Jump to content

Save XLS to server


MOTIVECODEX

Recommended Posts

Hello all! Sorry for the lame question, but I need to save XLS to my server, but how?

 

Have removed parts that are not relevant to my question. This code saves the content into .xsl format and downloads it immediately to the desktop if run with your browser, but how can I save that file to the server?

 

With something like file_put_contents()? But how? 

 

And does 

$files = header("Content-Disposition: attachment;filename=export_".date('n-j-Y').".xls ");

 even work? ( the `$files =` part )  

 

Thanks for all the help!

<?php

// database connection etc goes here

/*
* START HEX writing to Excel format
*/

// Beginning of XLS file
function xlsBOF() {
echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
return;
}

// End of XLS file
function xlsEOF() {
echo pack("ss", 0x0A, 0x00);
return;
}

// Writes text to cells
function xlsWriteLabel($Row, $Col, $Value ) {
$L = strlen($Value);
echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
echo $Value;
return;
}

/*
* Database connection goes here
*
*
*
* [removed]
*
*
*/

/*
* Headers for the browser
*/

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
$files = header("Content-Disposition: attachment;filename=export_".date('n-j-Y').".xls ");
header("Content-Transfer-Encoding: binary ");


// Start the file
xlsBOF();

/*
* To keep it in order
*
* [removed]
*
*
*/

// On first row
$first = true;

while( $qrow = mysql_fetch_assoc( $qr ) )
{
if( $first )
{
// [removed]
}

// goes through the data
foreach( $qrow as $k => $v )
{
// write it in the file
// [removed]
}
// reset col and goto next row
$col = 0;
$row++;
}

xlsEOF();
exit();

?>
Edited by MOTIVECODEX
Link to comment
Share on other sites

XLS or xsl??

 

Do you mean "spreadsheet", aka, a client file? Saving a file to your server (aka, 'uploading') is simple. You can find many examples of how to code this. Try google. :)

 

As for setting a php var with the header statement you proposed, I don't think that works. But I've been wrong many times.

Link to comment
Share on other sites

XLS or xsl??

 

Do you mean "spreadsheet", aka, a client file? Saving a file to your server (aka, 'uploading') is simple. You can find many examples of how to code this. Try google. :)

 

As for setting a php var with the header statement you proposed, I don't think that works. But I've been wrong many times.

 

Yes I mean xls, from excel. My code works perfect and it does what I want it to do, the only thing is that it does not save the file on my server. 

Link to comment
Share on other sites

If this script is running on your server and you want to save the file on that server, there is no upload involved.

 

You have a couple of options:

 

1) Instead of echoing the data directly, collect it in a string. When you are done, write the string to a file and write it to the browser (i.e. echo).

 

OR

 

2) Again, do not echo the data, write it directly to a file on the server. When you are done, send the file to the client (readfile).

 

OR

 

3) Use the Output buffering functions (see ob_start) to collect what you are outputting and then grab the buffer, write it to a file, and echo it as well.

 

 

Unless it is a HUGE amount of data, I would choose option 1. If the file is or can be very large, I would use option 2. I do not recommend option 3; eventhough it will be the easiest to implement into what you have, it can create issues and make it difficult to debug.

 

 

$files = header(...) - The header function sends a header. It does not return a value, so $files there will be null and the header will be sent. The assignment is useless. What are you trying to accomplish there? You never use $files in the code you showed.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.