I have a simple php file for example:

<?
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=file123.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "test";
?>

What I want to do is export php result and save it in excel file. If I run this from web browser like IE,firefox, I have no problem to generate excel file on my local machine.
However I want to run this from command line at background, instead of web browser, now if I open command prompt box and run it, it just print result "test" on command box, there is no export file generated.

Why??? how can I fix this issue?

Any advice will be appreciated.

Dani AI

Generated

The CLI and a browser are different clients. As discovered, sending a download via HTTP headers only works when a web client receives that response. Running the same PHP on the command line (or in cron) just writes the response to stdout — there is no browser to interpret Content-Disposition and create a file. For automation, either generate the file on disk from PHP itself or have cron fetch the HTTP URL and save the response.

Preferred solution for cron: make the exporter write a file to disk when run under CLI. Detect the SAPI and save output to an absolute path (so permissions and working directory are explicit). Using the CLI-friendly flow also lets you switch from simple tab/CSV output to a proper spreadsheet library if needed. See the PHP docs for detecting CLI and writing files: php_sapi_name and file_put_contents. A small guard and write step keeps the web behaviour unchanged while letting cron create persistent files.

Alternate quick approach: call the existing HTTP export from cron with a download tool (curl or wget) so the server-side headers are handled as they are for browsers. Use the options that honor server filenames or explicitly provide an output location so the result lands where cron can find it.

Practical tips: use absolute paths for PHP and files in crontab, ensure the cron user has write permission to the target folder, test the CLI run manually before scheduling, and add logging and simple file-locking to avoid overlapping runs. If you need formatted XLSX (not just tab/CSV), consider a library such as PhpSpreadsheet (PHPOffice) to produce real Excel files rather than relying on .xls renamed text. 's note about producing well-formed tabular output remains relevant when you generate the file content.

Recommended Answers

All 4 Replies

It looks like your last line is wrong. Also, try this version of the first line. This is what I use:

header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=extraction.xls");
header("Pragma: no-cache");
header("Expires: 0");
print $data;


$data needs to hold the tab delimited text file that you create. Your line just tells the routine to print the string "test". You are not passing any data.

You have to format the data. Separate your fields with chr(9) and end each line with chr(9).

Thanks for reply.
I tried this, it works ok from web browser, but not from command line, it doesn't generate excel file.
Actually this $data is the dynamic php web page I created, I want to save this php page into excel file, from command line.

Spreadsheets are for tabular data. How do you plan to break a web page up into cells? Are you talking about data that you are displaying on a PHP page?

I'm not sure how you would do this from a command line.

The php page contains table that display dynamic data retrieved from mysql database, I have no problem to display this table on web browser and convert it to excel file, by manually.
Now I need to set cronjob to run this php script at backend to retrieve data from mysql DB and generate excel file in weekly basis, I got stuck here.
Any idea?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.