Page 1 of 1

Unable to download file - PHP

Posted: Tue Jan 05, 2016 12:54 pm
by publicGenome
Hi Members,

I'm trying to download file from PHP page, which apparently is in vain. Apologies for a goofy problem.

My code looks like:

Code: Select all


<?php 
header('Content-Description: File Transfer');
header('Pragma: public');

?>
<a href="<?php echo "/Users/user_name/Desktop/otu_table_tabseparated.txt";?>"> Download</a>

I must be barking at a wrong tree. :(

The file is present at the location. On click of download link, my page navigates to:
It (above ) is not a valid path. Also, I do not want the want to refresh/navigate page. :banghead:

I've .htaccess file

My local host points to ~/Sites directory

This code is the simplest I could come to learn on how to allow user download file, which unfortunately didn't work.

Please guide.

Re: Unable to download file - PHP

Posted: Tue Jan 05, 2016 1:05 pm
by publicGenome
Update:
When I put the file at the same location where my .php is present it works partially.
By partially, I mean the file is displayed in the browser. My code is:

Code: Select all

<?php 
header('Content-Description: File Transfer');
header('Pragma: public');

?>
<a href	="<?php echo "otu_table_tabseparated.txt";?>"> Download</a>
It wouldn't ask for save dialog box.

Re: Unable to download file - PHP

Posted: Tue Jan 05, 2016 1:32 pm
by Celauran

Re: Unable to download file - PHP

Posted: Tue Jan 05, 2016 1:39 pm
by publicGenome
Hi C,
Thank you for your reply to my query. I used :

Code: Select all

header('Content-type: application/txt'); 

header("Content-Disposition: attachment");
both (used one at a time) these download my .php itself instead of a of the actual file.

Re: Unable to download file - PHP

Posted: Tue Jan 05, 2016 1:51 pm
by Celauran
Linking to the file directly is going to display it according to the browser's preferences. If you want to force download, you'll need a dedicated download endpoint to which you can pass the file path. This endpoint is what will use the headers referenced above.

Re: Unable to download file - PHP

Posted: Tue Jan 05, 2016 2:09 pm
by Christopher
I just showed this in another thread yesterday:

Code: Select all

header('Content-type: application/txt'); 
header("Content-Disposition: attachment");
readfile('/path/to/otu_table_tabseparated.txt');
Also, if it contains tab-separated data, you might want to set the Content-type to one of the CSV or Excel MIME types so it opens in a spreadsheet application.

Re: Unable to download file - PHP

Posted: Tue Jan 05, 2016 2:19 pm
by publicGenome
Christopher wrote:I just showed this in another thread yesterday:

Code: Select all

header('Content-type: application/txt'); 
header("Content-Disposition: attachment");
readfile('/path/to/otu_table_tabseparated.txt');
Also, if it contains tab-separated data, you might want to set the Content-type to one of the CSV or Excel MIME types so it opens in a spreadsheet application.
Hi Christopher,

Thank you for your reply.

I used the exact code of yours, but it gives me complete php file

Code: Select all

header('Content-type: application/txt');
header("Content-Disposition: attachment");
readfile('otu_table_tabseparated.txt');
I was prompted with a dialog box, save location.

My goal is to allow user to click on download link, and save it. Something like:

Code: Select all


<a href	="<?php echo "otu_table_tabseparated.txt";?>"> Download</a>

Re: Unable to download file - PHP

Posted: Tue Jan 05, 2016 2:53 pm
by publicGenome
Update:

I'm able to get a simplest download using:

Code: Select all

<a  href="<?php echo "otu_table_tabseparated.txt";?>" download > Click me</a>
`download` keyword is important here.

References:
http://www.w3schools.com/tags/att_a_download.asp

http://www.w3schools.com/tags/tryit.asp ... a_download

Thanks.

I'll work on the PATH now. :x

Thank you.

Re: Unable to download file - PHP

Posted: Tue Jan 05, 2016 3:17 pm
by Celauran

Re: Unable to download file - PHP

Posted: Tue Jan 05, 2016 4:40 pm
by Christopher
Ok, now I am confused. Say you have this three file website:

Code: Select all

index.html
otu_table_tabseperated.php
otu_table_tabseperated.txt
File: index.html

Code: Select all

<a href="otu_table_tabseperated.php">Download</a>
File:otu_table_tabseperated.php

Code: Select all

header('Content-type: application/txt');
header("Content-Disposition: attachment");
readfile('otu_table_tabseperated.txt');