0

Is it possible using the header() function or perhaps another php function to pass a file to the browser locally?

I am currently using Chrome. I have the Office Editing for Docs extension installed which essentially allows me to open Word docx files locally into my Chrome browser by passing the full directory and file name into the URL address bar of Chrome. This essentially opens the Word file in Chrome. I would like to accomplish this task in php

I have tried the following below but no luck.

header( 'Location: file://c:\users\jbloggs\desktop\test.docx' );

I know the header() function is primarily used for redirecting to a web page

header( 'Location: http://www.google.com' );

Any help much appreciated.

David Egan
  • 424
  • 2
  • 8
  • 23
  • 1
    PHP is server-side and is not capable of opening files on the client-side. – Jay Blanchard Aug 04 '16 at 14:03
  • 3
    You need to get in the habit of [accepting answers](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) which help you to solve your issues. You'll earn points and ***others will be encouraged to help you***. – Jay Blanchard Aug 04 '16 at 14:03
  • 1
    Question is: *Why?* and do you have a webserver/php installed? what are you really wanting to do here? – Funk Forty Niner Aug 04 '16 at 14:05
  • `header()` is used for lots of things. In fact for any headers you want to send to the browser – RiggsFolly Aug 04 '16 at 14:07
  • To my knowledge, Chrome will generally reject `file:///` URLs from a website. Also note the triple-slash - http://superuser.com/questions/352133/why-do-file-urls-start-with-3-slashes – ceejayoz Aug 04 '16 at 14:08
  • 1
    [Check out tdammer's answer here](http://stackoverflow.com/questions/4346117/how-can-i-view-open-a-word-document-in-my-browser-using-with-php-or-html) Hope it helps! – Bilal Ahmed Aug 04 '16 at 14:08
  • ^ which is what I thought they (really) wanted to do. Hard to say though, we're all here commenting with no response (yet). – Funk Forty Niner Aug 04 '16 at 14:09
  • 1
    Ok David; you've been given an answer (and one that stands on getting deleted). Ask them or ping one of us. I've spent enough time here. Or, wait for a magica that you can accept as @JayBlanchard already pointed out. As it stands, all your previously asked questions are still considered as open/unsolved and this one stands to be the same thing, so I'm out, good luck. – Funk Forty Niner Aug 04 '16 at 14:12

1 Answers1

1

It doesn't work this way, because you are trying to redirect from a remote server to a local file path. Chrome doesn't accept this because of security considerations. Note that it doesn't matter whether your web server is running on the same physical machine, it is seen as a separate server from your local file system. You can however accomplish this task using normal HTML:

<a href="file://c:\users\jbloggs\desktop\test.docx">Document</a>

If you save this to a static HTML file and open it in your browser you should upon click be redirected to the document. If you want a direct redirect, use JavaScripts window.location You cannot however serve the file from a HTTP Server, like mentioned above.

If you want to do so, you have to serve the .docx file from your server as well, by including it as static content and then linking to it via HTTP as well.

Hope this helps!

Peter
  • 1,361
  • 15
  • 19
  • But why would PHP have to know? It just needs to pass that value on to the browser. It can happily redirect to `magnet:` URLs and whatnot, surely it doesn't have native support for that? – Siguza Aug 04 '16 at 14:24
  • Yes I oversimplified and made my answer faulty. See my edit. I'm actually not sure whether it's completely impossible to make the redirect via php but it's certainly not best practice. Best practice would be to serve the .docx file from your server as well, instead of your local file system. – Peter Aug 04 '16 at 14:55
  • I can fully agree with that. I was just wondering why other URLs worked but not file:// ones. It makes sense that this would be a browser security feature though. – Siguza Aug 04 '16 at 15:04