Open a file with a href

Joined
Oct 14, 2023
Messages
3
Reaction score
0
Hi,
how do I open a file and I see it on the screen ?

I have the following code:

<a href="C:\test\cani.docx">Open the file</a>

When I click on Open the file, it does not work.

I'm using PHP with XAMPP and my personal computer.

Thanks.
 
Joined
Jul 4, 2023
Messages
366
Reaction score
41
You can use the file:// protocol to open local files directly from a browser, but it might not work reliably due to security restrictions imposed by modern browsers. Browsers generally limit access to local files for security reasons.

To use the file:// protocol to open a local file in a browser, you can construct your link like this.
HTML:
<a href="file:///C:/test/cani.docx">Open the file</a>

Make sure to use three forward slashes (///) after file: to indicate the file protocol and provide the full path to your local file.

However, keep in mind that different browsers may have varying degrees of support for opening local files using this method, and it may not work as expected or at all, depending on the browser's security policies.

A more reliable approach, as mentioned earlier, is to move the file to a directory accessible by your web server (e.g., within your XAMPP's htdocs directory) and use a relative or absolute URL as previously described. This method is more likely to work consistently across different browsers and provides better security and control over access to the file.
 
Joined
Oct 14, 2023
Messages
3
Reaction score
0
I tried

<a href="file:///C:/xampp/htdocs/dashboard/docdigitalizzaz/documento/test/cani.docx">Open the file 3</a>

with Internet explorer, mozilla, chrome but it does not work.

The document cani.docx is in the xampp htdocs dashboard folder.
 
Joined
Jul 4, 2023
Messages
366
Reaction score
41
move the file to a directory accessible by your web server (e.g., within your XAMPP's htdocs directory) and use a relative or absolute URL
try like this

htdocs-xampp.png

HTML:
<style>
  html,
  body {
    padding: 0;
    margin: 1rem;
  }
  h4 {
    margin-bottom: .5rem;
  }
  a {
    display: block; 
  }
  a + a {
    margin-top: .5rem;
  }
</style>

<h4>Relative</h4>
<a href="dashboard/docdigitalizzaz/documento/test/cani.docx">Open the file 3</a>
<a href="./dashboard/docdigitalizzaz/documento/test/cani.docx">Open the file 3</a>

<h4>Absolute</h4>
<a href="http://localhost/dashboard/docdigitalizzaz/documento/test/cani.docx">Open the file 3</a>

browser.png
 
Last edited:
Joined
Oct 14, 2023
Messages
3
Reaction score
0
Ok, thanks.

They all work but the file appears in the Download folder using Chrome or Internet Explorer.

I need to open the file directly with a click of the mouse without the download.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top