What are the ways to make an html link open a folder
Solution 1
Do you want to open a shared folder in Windows Explorer? You need to use a file:
link, but there are caveats:
- Internet Explorer will work if the link is a converted UNC path (
file://server/share/folder/
). - Firefox will work if the link is in its own mangled form using five slashes (
file://///server/share/folder
) and the user has disabled the security restriction onfile:
links in a page served over HTTP. Thankfully IE also accepts the mangled link form. - Opera, Safari and Chrome can not be convinced to open a
file:
link in a page served over HTTP.
Solution 2
The URL file://[servername]/[sharename]
should open an explorer window to the shared folder on the network.
Solution 3
A bit late to the party, but I had to solve this for myself recently, though slightly different, it might still help someone with similar circumstances to my own.
I'm using xampp on a laptop to run a purely local website app on windows. (A very specific environment I know). In this instance, I use a html link to a php file and run:
shell_exec('cd C:\path\to\file');
shell_exec('start .');
This opens a local Windows explorer window.
Solution 4
Using file:///// just doesn't work if security settings are set to even a moderate level.
If you just want users to be able to download/view files* located on a network or share you can set up a Virtual Directory in IIS. On the Properties tab make sure the "A share located on another computer" is selected and the "Connect as..." is an account that can see the network location.
Link to the virtual directory from your webpage (e.g. http://yoursite/yourvirtualdir/) and this will open up a view of the directory in the web browser.
*You can allow write permissions on the virtual directory to allow users to add files but not tried it and assume network permissions would override this setting.
Solution 5
make sure your folder permissions are set so that a directory listing is allowed then just point your anchor to that folder using chmod 701 (that might be risky though) for example
<a href="./downloads/folder_i_want_to_display/" >Go to downloads page</a>
make sure that you have no index.html any index file on that directory
Comments
-
Sebastien Lachance almost 2 years
I need to let users of an application open a folder by clicking a link inside a web page. The path of the folder is on the network and can be accessed from everywhere. I'm probably sure there is no easy way to do this, but maybe I'm mistaken?
-
Travis about 15 yearsThis answer works. The "directory listing allowed" part is very important. If it's not allowed, you can enabled it but it's different for every server application.
-
Stefan Steiger about 11 yearsin IE only if the UNC path and the website containing the link are in the same domain, that is to say only in the intranet.
-
ZeekLTK almost 10 yearsI think this is no longer correct - newer versions of IE seem to block this behavior just as Chrome/Safari/etc.
-
Schmuli almost 10 yearsThis is relevant today, as Chrome and newer versions of IE will block access to local file:// resources from non-file web pages. Additionally, this can be setup to run in IIS Express, although it must be added and run manually.
-
Hohohodown about 9 yearsI just tried this in IE 11 and you can in fact open a local file without downloading it (as in open an already existing file path). This still does not exist in chrome.
-
Zach Johnson almost 9 yearsYou can get this to work in Chrome via LocalLinks Chrome extension . Thanks to this StackOverflow answer
-
clhy over 8 years@Andrew Duffy, is there any kind of documentation that says Google Chrome is blocking this behavior ? I would like to know more about this
-
HattrickNZ over 6 yearsthis seems to give a slightly different answer.
-
Stephen R almost 5 yearsPromising, but when I run this in Firefox the tab hangs. (Seems to be churning the session or something, because I can access other sites, but this site seems to be hung -- even in other tabs!)
-
Lucas Taulealea almost 5 yearsI just tested it in Firefox, it works for me, but I can't specify the folder in which to open, it only opens in the root directory of the php file.
-
Dave Sottimano over 3 yearsChrome gives me an error: Not allowed to load local resource: <URL>
-
Nagaraja JB over 3 years@Dave Sottimano I tested it just now. Works for me in Google Chrome, Version 84.0.4147.135 (Official Build) (64-bit)
-
Kaddath about 3 years@LucasTaulealea I wonder why this answer has not been upvoted more. I would just add a little correction: the
cd
part seems unnecessary, in my case using onlyshell_exec('start C:\path\to\file');
works and supports both a folder or file -
JamesHoux almost 3 years@Kaddah It probably hasn't been upvoted more because this solution only works when the webserver is running on the same computer as your web browser. Most people are probably looking for ways to serve Windows Share Files from a corporate network web site.
-
Shawn over 2 yearsthis just downloads the file for me, vs running it in its native app