PHP display PDF from internal file server - php

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 "' . "Work Instruction Link"
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 ''.$wi_num.'';
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);
?>

Related

How do I prevent a CDN Location redirect for download from Azure from altering the browser location?

I'm using the PHP Azure library to create a SAS token for delivering files (File Storage, not blob, and not public) to an authenticated user of a site after they click a link. The link leads to a PHP page where I create the SAS and then issue a Location: header to invoke the downloads. There are no known issues with anything leading up to the final completion of the download.
In the attached image the steps 1-4 are repeated.. one for each file selected.
The issue is that as the URLs are external to the site (CDN) the Location: $url + sas is used. This alters the browser location but is not reflected in the browser... the headers push the file as an attachment.
As mentioned, he user can continue to click other download links on the originating page and they work fine (this is desirable) however the first time they select any link on that same page to go elsewhere on the originating page/site (shown as clicking "home" in the example, the browser true location is revealed (accountname.file.core.windows.net/etc ) and the user gets a Resource not found error originating from the Azure File Storage server. (shown under the chart)
7.10.2017 added this image to illustrate the process used:
Link to SAS process chart
I'm slightly at a loss on how to push a URL download from a CDN without the browser location getting "reset" in this manner. I've done this from local servers/files many times but the Location: $url/SAS aspect is a different dynamic. In my mind, this should behave no differently than pointing the browser to a publicly accessible URL with an EXE that pushes the download, and still allows the user to say on the originating site. I'm starting to think this should be a redirect instead of Location but I haven't found very many Azure/SAS/File Storage examples to back this up. I have to be missing something simple here but it's escaping me.
I am not sure if I get it correctly, you seem to wanna download the file from Azure storage without changing address in the browser by using PHP script.
Basically, you can use file_get_contents() function as below to achieve this.
header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename=$filename");
$response = file_get_contents($SASUri);
echo $response;

Serve external file download with php

I am trying to make a personal youtube video downloader using some snippets i found on the web. I get a url which is given by youtube which is only useful for the ip that requested it, so it is only working on localhost, because when its on a web server, download fails since the IP that requested the download link (web server one) is not the same as the client downloading it (me). My question is if there is some kind of way to:
a) Download video to web server and then download to my pc from there (not the best one)
b) Make youtube think that the webserver is downloading it when in real its me who is downloading the video using the web server.
URLs look like this:
http://o-o---preferred---ams03s12---v13---lscache7.c.youtube.com/videoplayback?upn=TGDxfz8IZxI&sparams=cp%2Cgcr%2Cid%2Cip%2Cipbits%2Citag%2Cratebypass%2Csource%2Cupn%2Cexpire&fexp=917000%2C903802%2C901425%2C920917%2C922401%2C920704%2C912806%2C924412%2C913558%2C912706&key=yt1&itag=18&ipbits=8&signature=4F0668E4E49852653FA995D28FECC455B552327D.3DEB3B58D1C5343B5EA03B7DA21355326EF29B7E&mv=m&sver=3&mt=1347451352&ratebypass=yes&source=youtube&ms=au&gcr=es&expire=1347475934&ip=95.39.206.66&cp=U0hTTFRSU19ITkNOM19MTFdKOkVhV2otM1FhTXNj&id=2b4258153228ebc9
If you change the IP in that url to yours will still dont work, I already tried with that.
So trying to find a way similar to what ianhales said, this is what I finaly made: Its simple and probably the best way but it wors and helps me to fix another problem which was setting name of the file with the name of the correspondient youtube video. Here is it:
$url=$_POST['url'];
$filename=$_POST['name'];
header('Content-type: video/mp4');
header('Content-Disposition: attachment; filename="'.$filename.'"');
echo file_get_contents($url);
Where $url is the large url I posted on my question and $filename is the name of the video.
Maybe it will be helpful for someone so Im leaving it here.

php code turned gibberish?

I went to edit this PHP file - it's supposed to generate those captcha security images on contact forms - the images werent working, so I was going to see if there was a broken path or something i could fix simply.
But when I opened the file it looked like this:
http://mydomainsample.com/explosion/screenshots/Screen%20Shot%202012-05-17%20at%209.34.14%20AM.png
complete gibberish.
Is it possible this happened somehow while downloading the file from the server? I did not have ftp access to the site originally - we got control of the domain and transferred it from one host to another.
I used site sucker to backup the site before transferring, but it downloads php files as html files. you end up with filename.php.html.
in the past this has never turned the php into incomprehensible gibberish, so i dont understand why it did now.
The problem is, you cannot use programs like "site sucker" to get PHP files. This is because when you get a PHP file from a URL, the file is executed, and you're getting the output of the script. That's why you get .php.html.
It doesn't "turn the php into incomprehensible gibberish", the server runs the script and you're getting the output. Most of the time the output is HTML, which you can open as text. In this case, the script's output is a PNG file, thus why you see "gibberish". Rename the file to .png, then you'll see the image.
You need to get FTP access in order to get the PHP source.
That's a PNG image, not PHP source code.
The file actually looks like a PNG image, maybe you just downloaded output the PHP script has generated?

Download KML File Instead of View in Browser

I am working on a project which creates a KML File (just like an XML file, but used for Google Earth). Whats interesting is when I link to the newly created file, on my local machine, running XAMPP, the file is downloaded automatically, however when I move it to my web server (Linux, Fedora 8 on EC2) the link just loads the KML file in the browser as if it was an HTML file.
How can I force it to download the file instead of viewing it in the browser?
Here's how to link is displayed with PHP,
echo "<a href='$currentTime.kml'><img heigth=\"15px\" width=\"13px\" src=\"images/KML_Icon.gif\" /> Download</a>";
Any advice would help, thanks!
What you need to do is to specify the headers so the Browser knows what to do with the information that you are sending. So before you send anything to the browser you will need to specify the headers.
If you are linking to a specific file, then you will have to create a little "download manager" that will do this for you.
<?
header('Content-disposition: attachment; filename=the-name-you-want-them-to-see-in-their-download.pdf');
header('Content-type: text/xml'); //Since KML files are based on XML this is probably the best Content type to send to the user.
readfile('the-file-you-want-to-present')
?>
That should do it.
Thank you for your guys' input, but Oded had the answer regarding the mime types.
On the server there's a file called mime.types which didn't contain the mime type for a KML file, I added in
application/vnd.google-earth.kml+xml
And it now downloads the file instead of loading it in the browser, by the way apache needs to be restarted once you have made the changes.
I had this a long while ago, I used a method similar to this:
http://webdesign.about.com/od/php/ht/force_download.htm

PHP: prompting download from ftp?

hy guys,
i really need your help. i've succesfully connected to ftp server via php.
i'm listing all files that are on the server. if i click a file the browser should prompt a download window to download the file.
i've absolutely no idea how to do that. which method am i going to use. ftp_get kind of confuses me. it says i have to declare a local_file as well. i just want a file on the server to download to my harddrive.
how can i do that?
regards matt
The remote file has to first be downloaded to your server before you can send it to the user. It's invisible to the user, but you don't have a choice. PHP won't let the browser talk directly to the FTP server.
Create a separate php script that calls ftp_get for a specific file, stores it temporarily to your server to allow the user to download it.
Something like:
<?php
//assume the page was called like download.php?filename=downloaded.pdf
header('Content-Disposition: attachment; filename="'.$_GET['filename'].'"');
$tempFile = 'temp'.rand();
ftp_get($ftp, $tempFile, $_GET['filename'], FTP_BINARY);
readfile($tempFile);
You may add code to delete the tempFile too.
If you provide a link to a file that can't be read by the browser (such as a php file, audio, video, etc.) it will ask you to download the file.
The other way is to use PHP headers on a page and print out the page, and link to that page. http://www.ryboe.com/tutorials/php-headers-force-download

Categories