Force File Open In Word

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Force File Open In Word

Post by icesolid »

I am having an issue with linking to Word documents on my site.

I would like to have every users computer open the Word doc in Word when they click on an link to a Word document on my site.

However, some users browsers open the document right up in the browser, some lag and take a real long time to open the document and some do exactly as a like and open right up in Word.

I know that this is a setting in the users browser, however is there anyway to force this behavior from my PHP or JavaScript code?

Any suggestions?
thewebdrivers
Forum Commoner
Posts: 41
Joined: Fri Aug 17, 2007 3:32 pm
Location: india
Contact:

Post by thewebdrivers »

mmmm.... i don't really think there is anything that can be done.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

What you'll need to do is create a php script to handle file downloads.

From our useful posts thread, viewtopic.php?p=53557#53557

Code: Select all

$file = fopen($filename, 'r');

//set some HTTP headers
Header('Content-Type: application/x-octet-stream');
Header('Content-Transfer-Encoding: binary');
Header('Content-Length: ' . filesize($filename));
Header('Cache-Control: no-cache, must-revalidate'); //HTTP 1.1
Header('Cache-Control: post-check=0, pre-check=0', false); //HTTP 1.1
Header('Pragma: no-cache'); //HTTP 1.0
Header('Content-Description: Whatever the file is');
Header('Content-Disposition: attachment; filename="'.$filename.'"');
Header('Title: ' .$filename());

while(!$feof($file))
     print(fread($file, 4096));

fclose($file);
However, you still need to specify your $filename, which is the path to your word file.
Post Reply