Open a direct file on the hard drive from firefox

2020-01-26 11:15发布

问题:

I have a small php system i created for myself. This system contains a few .doc and .docx file documents. I want to be able to open them directly from the browser and not downloading them.

I created a link: <a href="file:///c:/mysite/myfile.docx">myfile</a> But FireFox does not open the link. While href="http://localhost/myfile.docx" works fine - it offers me to download the file.

Any ideas how can i do it? Maybe change something in firfox options / security settings? And if you are at it, is there any chance to do it in IE8?

Thanks.

回答1:

Although i still think it is a programming question, it was answered here: https://superuser.com/questions/103026/open-a-direct-file-on-the-hard-drive-from-firefox-file

Both Firefox and IE8 support the File URI scheme.

Here are some examples valid for Windows systems, referring to the same file c:\WINDOWS\clock.avi

file://localhost/c|/WINDOWS/clock.avi
file:///c|/WINDOWS/clock.avi
file://localhost/c:/WINDOWS/clock.avi
file:///c:/WINDOWS/clock.avi

While the last is the most obvious and human-readable, the first one is the most complete and correct one.

Apparently (from the same url):

Mozilla browsers refuse to follow file URLs on a page that it has fetched with the HTTP protocol.

but:

Mozilla browsers can be configured to override this security restriction as detailed in Mozillazine's "Links to Local Pages Don't Work".



回答2:

Access to local files in Firefox can't be done unfortunately due to security restrictions. As far as I know (I have asked here repeated times myself) it's not even possible to allow select sites through a configuration setting or similar.

There is a workaround (ponentially unsafe and to be handled with care), you could register a custom protocol as outlined in this question.



回答3:

Which syntax did you use? The correct syntax for e.g. c:/path/to/file.ext would have been file:///c:/path/to/file.ext. Edit: not relevant anymore as OP has edited his question to include the syntax.

Note that this doesn't work at all if the file is stored at the server machine which in turn is a physically different machine than the client machine (where the webbrowser runs).



回答4:

I used symlink for windows added that file to my resources folder by creating symlink. How to create symlink shown here for windows http://www.sevenforums.com/tutorials/278262-mklink-create-use-links-windows.html

That worked for me while same problem. Cheers!



回答5:

Security restrictions are a big issue when trying to access a file outside of your project. What I would recommend (if you are using IIS 7+), is to create a virtual directory to the folder that contains your docs on your C drive. Then you can access your files from the C drive by using the VP name and the host/localhost will be included in your url rather than the "file:///".

However, if you are using IIS Express, you will have to manually add the virtual directory by updating the "applicationhost.config" file. Take a look at this link for more detail on how to set that up.

http://bhagirath-j-patel.blogspot.com/

Hope this helps.



回答6:

My working code to open locally uploaded Files via Firefox (51.0) and the actual Internet Explorer (11).

$pfad = trim(strip_tags(stripslashes(urldecode($_REQUEST['pfad']))));
$datei = trim(strip_tags(stripslashes(urldecode($_REQUEST['file']))));
exec ('start c:\\WINDOWS\\system32\\explorer.exe /select /seperate /n, /e, '.str_replace('/','\\',$pfad).utf8_decode($datei));

saved as open.php

"pfad" is a path BELOW the local Webroot (in my case customer folder) while "file" is - of course - the file which I want to open.

I call it like so: ...href="open.php?pfad=<my_path>&file=<the_file>"

where both parameters are urlencoded