简体   繁体   中英

PHP serve a link to a file when pointing to same server

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>" 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?

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);
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM