Unable to download file - PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
publicGenome
Forum Contributor
Posts: 110
Joined: Thu Apr 16, 2015 7:55 am

Unable to download file - PHP

Post 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.
publicGenome
Forum Contributor
Posts: 110
Joined: Thu Apr 16, 2015 7:55 am

Re: Unable to download file - PHP

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Unable to download file - PHP

Post by Celauran »

publicGenome
Forum Contributor
Posts: 110
Joined: Thu Apr 16, 2015 7:55 am

Re: Unable to download file - PHP

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Unable to download file - PHP

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Unable to download file - PHP

Post 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.
(#10850)
publicGenome
Forum Contributor
Posts: 110
Joined: Thu Apr 16, 2015 7:55 am

Re: Unable to download file - PHP

Post 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>
publicGenome
Forum Contributor
Posts: 110
Joined: Thu Apr 16, 2015 7:55 am

Re: Unable to download file - PHP

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Unable to download file - PHP

Post by Celauran »

User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Unable to download file - PHP

Post 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');
(#10850)
Post Reply