开发者

Opening a Word document using the PHP COM object

开发者 https://www.devze.com 2022-12-14 21:44 出处:网络
I\'ve got some code that generates a word document, as follows: $word->Documents [1]->SaveAs ( $localDir . $filename );

I've got some code that generates a word document, as follows:

$word->Documents [1]->SaveAs ( $localDir . $filename );

Now, I was kinda hoping that I could now open the file once it's saved by doing the following:

$word->Documents->Open($remoteDir . $filename)

// remotedir = 'word/', so for example the above would be '/word/document1.doc'

But it seems to open it on the host machine, and not the users! Is there anyway to open it on the user's machine and not on the server?

edit: Just for clarity it will be used exclusively on an intranet by a single user that will be on a Windows machine at all times, with W开发者_JS百科ord etc installed... just want to try and make her life a little easier!

Thanks


I think you are fundamentally mistaken about what runs where. PHP is a purely server side language. You can not use it to open a file on the client's PC so that the user has an opened instance of Word in front of them.

You can maybe achieve that through client side Scripting, namely in VBScript or some other Microsoft scripting flavour. Be prepared for massive obstacles and incompatibilities, though, because such things are blocked for security reasons by default in all browsers, and sometimes those blocks cannot be circumvented even with special settings ("Trusted sites") in the client browser.

You may be able to display the document in the user's browser as an embedded HTML object.

The most simple thing really may be generating the file, and offering it to the user as a download. The user can then save it, and open it. Job done.


/word/document1.doc is the path for a file in the server, not in the client. On Windows, supposing that the file sharing is enabled for the client PC, then you can use a path such as \\IP\word\document1.doc, where IP is the IP of the client PC.
You can get the IP of the PC connecting to the server with $_SERVER['REMOTE_ADDR']; $_SERVER['REMOTE_HOST'] is the result of a DNS reverse lookup, which could return the same value of $_SERVER['REMOTE_ADDR'], in your case.

Probably PHP will not open remote files if it has not been set to do so (there is a directive or that).

If directly accessing the shared file from the COM object doesn't work, then you can copy the file from the client PC to the server in a temporary file, and then give that file to the COM object. In this way, if there are any errors while accessing the networked file, you should be able to get them.

I find strange, anyway, that passing a network file path you get a local file. Are you sure the COM object is not copying of the server the file it finds at the remove file path passed? Did you try with a different file? If that happens with different files too, then we are missing something; I would find strange that for all the network files you try to open, there is already a local file with the same name. Try also renaming the network files.

0

精彩评论

暂无评论...
验证码 换一张
取 消