How to auto-create and save spreadsheet document on server.

I am writing a PHP script that pulls data from a MySQL table and converts it into an Excel spreadsheet. This process functions correctly -- I can output it as a downloadable file to the user's browser.

Now, the final step is what I am having trouble with. Instead of outputting the the browser, this script needs to SAVE the data as an excel file on the web server. The script will be run nightly as a cron job.

I am attaching a code snippet that shows how I am combining the headers and the data ($data) for the output.

Instead of outputting to the browser, how can I output to a saved file on the server? Thanks!
header("Content-type: application/xls");
header("Content-Disposition: attachment; filename=Beta_Product_Master_Table.xls");
header("Pragma: no-cache");
header("Expires: 0");
echo $header."\n".$data;s

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I could possible be missing the point here, but if you want to save the file (and have the data part figured out already) though a cron job, you could do the following...

(Note: I've only actually done this with PDFs)

See Code Snippet...

Then in SSH:

# crontab -e
 ...browse to bottom of crontab...
0            23      *       *       *       lynx -dump >/dev/null 2>&1
(ZZ to save)

This will execute the target script 11:00 PM nightly.
$fname = "/path/to/".mktime().".xls"; //added timestamp to make unique
$data = "data\n data\n data\n";
$file = fopen($fname, "w");
fwrite($file, $data);
mail('', 'new XLS created', "$file was created on server");
You will need to give write permissions to both the 
script calling fopen() as well as the directory which 
you would like to save the file. Cheers.

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mapennAuthor Commented:
Perfect. Thank You so much!
mapennAuthor Commented:
Thanks it helps a lot
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.