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

Posted on 2008-11-19
Last Modified: 2013-12-13
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

Question by:mapenn
    LVL 9

    Accepted Solution

    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


    Author Closing Comment

    Perfect. Thank You so much!

    Author Comment

    Thanks it helps a lot

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to count occurrences of each item in an array.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now