Web Hosting Talk







View Full Version : PHP script to open up an excel document


Davey Gale
02-23-2005, 10:09 AM
Hi there

I have created an Excel workbook that i wish to be accessed from my website online. (the workbook is on my server space). Currently the link i have set up directly points to the Excel file. When I click the link it opens up the Excel worksheet within a Internet Browser. I dont want this, i want the user to be given a prompt which asks whether they want to open or save the document, if they click open i want it to open in Excel itself and not a browser window.

Can i setup a php script that when clicked it brings up a menu which asks whether you want to open, save or cancel. (as opposed to directly linking to the file). When you click open i want Excel itself to open up on the PC - not through a browser.

The following unrelated script I use causes the Excel program to open and display the contents of 1 of my mysql databases:

<?php
define(db_host, "localhost");
define(db_user, "test");
define(db_pass, "test");
define(db_link, mysql_connect(db_host,db_user,db_pass));
define(db_name, "test");
mysql_select_db(db_name);
$select = "SELECT * FROM test";
$export = mysql_query($select);
$fields = mysql_num_fields($export);
for ($i = 0; $i < $fields; $i++) {
$header .= mysql_field_name($export, $i) . "\t";
}
while($row = mysql_fetch_row($export)) {
$line = '';
foreach($row as $value) {
if ((!isset($value)) OR ($value == "")) {
$value = "\t";
} else {
$value = str_replace('"', '""', $value);
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim($line)."\n";
}
$data = str_replace("\r","",$data);
if ($data == "") {
$data = "\n(0) Records Found!\n";
}
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=extraction.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
?>

Can i steal something from this which will give me the same effect? I just want the file to force the PC to load Excel independently - not have it load excel within an internet browser.

Is this possible? Any input would be brilliant.

runesolutions
02-23-2005, 02:00 PM
I may be wrong, but I'm not sure you can force a PC to do anything outside of the browser via server-side scripts. Seems to me that it would be a bit of a security compromise.

monaghan
02-23-2005, 02:08 PM
I do this in one of my apps to generate a Word doc and this pops a box asking to open or save. I guess it'll do the same in Excel

header( "Content-type: application/msword" );
header( "Content-Disposition: attachment; filename=\"myword.doc\"");

Davey Gale
02-23-2005, 02:33 PM
Hi thanks for the reply, is it a case of simply putting those 2 lines in a php script? Is there anything else that needs to go in there?

With that code is it generating a new word document and giving it a name, as opposed to opening an existing one? I've got the file already on the server - i wonder if my request is not possible?

runesolutions
02-23-2005, 02:56 PM
Yes, my apologies, I was misreading your post and thinking you were trying to do something on a client machine without their interaction.

That should work. It's basically just forcing a download (which a user can reject of course).

This:


header( "Content-Disposition: attachment; filename=\"myword.doc\"");


Will set the file name as it appears to the user.

I think you would need something like:


readfile(your_file_on_the_server);


to force the actual file to open.

Roy@ENHOST
02-23-2005, 06:41 PM
Your script is doing OK from what I know.
That will force a popup window asking if a user wants to save the file or open it using a known application for that extension.