CSV File displayed by browser insteed of prompting download - php

In my application, it returns a csv file location on my server through an ajax call of a php page.
I want the browser to prompt for download. I use
location.href = "report.csv";
But when the code is executed in a browser, it opens the csv file by itself and is not prompting for download.
This problem occurs with my LAMP server whereas in my laptop, which is configured in XAMPP, the same code works fine and prompts for download.
I have found that some programmer refer to use content-dispose as header in the page.
But I think I have missed some configuration in httpd.conf file.
Does anyone have the solution?
Thanks in advance.
Tathagata

set location.href = a.php (as example). then set header content of that a.php file to csv type and then echo your csv file(see google help). Maybe this will help you. see header content for csv file for html page from google.

Related

PHP display PDF from internal file 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 "' . "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);
?>

Android browsers wont open PDF in Browser when Content-Disposition is set in Header

On my Apache webserver i can server PDF files directly in the Browser with a simple Anchor link like:
Click to display PDF
This Works fine.
Now, i've got a lot of PDF's i want to Protect. So i moved them all outside the Web folder, and are serving them through a PHP download program - i called it download.php
To be sure that the pdf files are not downloaded as "download.php" i have to set the Content-Disposition Header to 'inline;filename="mysample.pdf"'. Inline because i want to open it in the Browser.
No Problem, all works fine. the files are downloading fine on Windows and Apple Browsers and are displayed IN the Browser, just like when its served directly from the Apache Server.
But my download.php doesn't work with Android Browsers... It's always just downloaded but not opened in the browser. You have to find the file in your messages and open it separately. When served directly from Apache it IS opened in the Browser.
It seems to be connected with Content-Disposition Header im setting in my download.php, because this is the only difference i see in the headers when download direct and indirect.
Anybody had the same problem?
Thanks
Per

How to get a file location url from a php redirect link?

I have a link to a download to a file that calls a php script before it starts downloading the file. Is it possible to somehow get the location of the file that is to be downloaded directly (maybe through some browser plugin)?
I need this because I want to use wget on another system to download the file directly. A problem might be authentication because I need to provide a username and password, but getting the file location URL is the first step I think.
Thanks,
Ivan
If the PHP script directly reads the file, no, you will never get the real location of that file.
If it redirects to it, yes, it's possible using the Firebug "net" tab or Live HTTP headers extension.

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