-1

I have an internal server running PHP and an internal file directory with some work instructions pdfs for our technicians to read from. I'm trying to add a link to those pdf files so the technicians can click on the link and open the pdf in the browser.

So I have echo "<a href=" . '"FILE://fileserver/path/to/workInstruction.pdf">' . "Work Instruction Link</a>"

and I get Security Error: Content at http://LocalServer:50563/workInstructions.php may not load or link to file:///path/to/workInstruction.pdf

I understand the security risk of not being able to access the local files of a user from a web page, but I don't understand why I can paste the same file url into the address bar and the file will display on the web page. How are those two mechanisms different?

Is there a way to make that link work while serving it from my PHP server, or am I just going about the problem completely wrong? Is there something wrong with my formatting or syntax that I'm not catching?

I noticed that the error response does not contain the server in the file name. I don't exactly know what that means, or why that is the case.

My question is not answered by this post: html-File URL "Not allowed to load local resource in the internet browser because 1. it is asking within classic ASP which is a very different technology from PHP and 2. While it does have the same error message it does not explain how to have a link to files hosted on the same server as the webserver.

I have also tried the answer to this question: Point a link to a certain server but it did not work for me when i try to convert "FILE://fileserver/path/to/workInstruction.pdf" to "Http://fileserver/path/to/workInstruction.pdf" I'm guessing because it's a file server and not a webserver that the pdf is located on.

To further pinpoint my question, is there a way in PHP to serve a link to a file hosted on a local server that the user can click and open a PDF in the browser?

BDeC
  • 27
  • 8
  • `file://` is local i.e the user's device, not local on the network. Either make `/fileserver/path/to/workInstruction.pdf` accessible or proxy it – Lawrence Cherone Mar 02 '21 at 19:01
  • The difference when you do it in your browser is you chose that yourself, a random web page didn't try to load it automatically. And don't forget that normally a web page doesn't get served from the same machine as the user of the browser, it comes from somewhere else online. therefore such a site has no business attempting to load things which are on the user's machine (which is where file:// always points to). If you want to link to a file from your website it must be done via http:// rather than file:// and the item must be served from a webserver just like the PHP files. – ADyson Mar 02 '21 at 19:03
  • `guessing because it's a file server and not a webserver`... essentially yes. if course it's possible to configure a virtual directory in your webserver which points to the physical path on your network server and therefore makes it available via HTTP (but be careful of security if that's an issue for you), you don't necessarily need to move the files. – ADyson Mar 02 '21 at 19:07
  • @ADyson That gives me some new things to google. Thank you. – BDeC Mar 02 '21 at 19:15
  • 1
    Alternatively you could implement a separate controller. The implementation of which would read the file in memory (or stream) from disk on the serverside and pass the file contents on to the client. – PtrTon Mar 02 '21 at 20:46
  • @PtrTon Thanks, I think that's basically what I ended up doing. The code I inherited did not follow any architecture or pattern so it has been difficult trying to translate what it's doing into MVC. – BDeC Mar 02 '21 at 22:42

1 Answers1

0

For anyone else coming to this looking for an answer I have found this answer worked for me: PHP-Display PDF on browser

Essentially I take the file path of the PDF I'm trying to load that I get from my fileserver and redirect to a new php file that reads the file and displays it in a new tab

The php file that contains the link will have something like this:

echo '<a href="workInstruction.php?filename=workInstruction.pdf&filepath=//fileserver/path/to/workInstruction.pdf"' . 'target="_blank">'.$wi_num.'</a>';

My workInstruction.php looks like this:

<?php
$filename= $_GET['filename'];
$filePath= $_GET['filepath'];

header('Content-type:application/pdf');
header('Content-disposition: inline; filename="'.$filename.'"');
header('content-Transfer-Encoding:binary');
header('Accept-Ranges:bytes');
@readfile($filePath);
?>
BDeC
  • 27
  • 8