how do i allow users to download files

Asked by:
bosso / 5 Points
Time:
2012-07-22 8:25 pm EST
Category:
File Management
Hits:
8,594
I've created a downloads folder in root, into which I uploaded a file. On my web page I created a link to the file. When I click the link on the published web page, it shifts me to the opened file (instead of downloading to where I usually send downloads).

I tried zipping the file, which works, but I'm on a Mac and PCers tell me a zip file from Mac includes 'other' things with the zipped file which I don't understand, but would instead like to know if there is a simpler way to do this.

So, how do I store the file so that web users can click on download and actually have the file downloaded to their computer instead of opening in a browser page?

Thanks

To ask this user for more information, please first login.

To submit an answer, please login.

ANSWERS

0

ScottM
Staff
16,266 Points
2012-07-23 4:09 pm EST
Hello bosso,

To force the browser to download a file instead of displaying in the browser, you will want to do two a couple of things:

1) log into your cpanel and enter the File Manager.

2) Create or upload a new, blank file named .htaccess (note the . in the front, that is necessary)

3) Place a line of code similar to the following for each type of document you want to force a download. In the examples below, I gave three different file types, .mov, .pdf, and .txt

AddType application/octet-stream .mov
AddType application/octet-stream .pdf
AddType application/octet-stream .txt

This should force your visitors browsers to download the file type for those extensions in that folder.

I hope this answers your question. If you have any more questions or information specific to the issue please leave a comment below so we can further assist you.

Best Regards,
Scott M

To submit a comment on this answer, please first login.

i made my script php file for file download and it's working perfect in locahost ,however in the server side it is not working i tryed to put this lines in my .htaccess

# Binary Compressed files and archives
AddType application/zip .zip
AddType application/x-gzip .gz
AddType application/x-gtar .gtar
AddType application/x-rar-compressed .rar

so if someone have idea how to fix this ,it will be so helpful for me
Eldewiny
15 Points

2014-02-06 4:03 pm EST
Hello Eldewiny,

What in particular are you attempting to accomplish with the file types? Are you attempting to handle them differently within your script for each?
JeffMa
2,342 Points
Staff
2014-02-06 4:41 pm EST
Thank you frist for your answer and here is the code what i use

<?php

function download($file){
$dir = './download/';
$path = $dir.$file;
if(!file_exists($path)){
die('Error');
}else{
header('Content-Description : File Transfer');
header('Content-Disposition : attachment; filename='.basename($path));
header('Content-Type: application/octet-stream');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
ob_start();
flush();
readfile($path);
exit;
}
}
if (isset($_GET['download'])) {
if (!empty($_GET['download'])) {
$file = $_GET['download'];
download($file);
}
}
?>

<a class="download-template" href="example.php?download=modern.rar">Download</a>
Eldewiny
15 Points

2014-02-06 8:02 pm EST
The error was being caused by extra spaces within your header lines. Rewriting it to the following works just fine:


<?php
function download($file){
$dir = './download/';
$path = $dir.$file;
if(!file_exists($path)){
die('Error');
}else{
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename='.basename($path));
header('Content-Type: application/octet-stream');

header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
ob_start();
flush();
readfile($path);
exit;
}
}
if (isset($_GET['download'])) {
if (!empty($_GET['download'])) {
$file = $_GET['download'];
download($file);
}
}
?>

JeffMa
2,342 Points
Staff
2014-02-06 11:24 pm EST
i update the code but i still get the same result i think it 's nothing todo with the code here is the link go down and under the image click the button and see the result

http://psdclass.com/example.php
Eldewiny
15 Points

2014-02-06 11:49 pm EST
These errors were being caused by the order in which your PHP includes were defined. As your header PHP include file was defined before the download script PHP include, the download script was unable to set the header information that is defined within it. I have reversed the two lines on your page so that the download script is called first, and as I see you have adjusted your previous code with that I provided previously, it appears to be working now.
JeffMa
2,342 Points
Staff
2014-02-07 12:47 am EST
OH MY GOD it is working thank you JeffMa so so much and nice to meet you
Eldewiny
15 Points

2014-02-07 12:55 am EST
It has been my pleasure. One thing I would like to add is to avoid using GET requests to defined the file to be downloaded. This could be exploited to read from any file on your site and could cause your site to become hacked. I recommend directly linking to the file for download instead.
JeffMa
2,342 Points
Staff
2014-02-07 1:22 am EST
thank you for the advice i will take care about the rest of the code
Eldewiny
15 Points

2014-02-07 2:52 am EST
Want to share this Question?

Related Articles

It looks like there are no related articles.
Would you like to ask a question about this page? If so, click the button below!
Need More Help?

Help Center Search

Current Customers

Email: support@WebHostingHub.com Ticket: Submit a Support Ticket
Call: 877-595-4HUB (4482)
757-416-6627 (Intl.)
Chat: Click To Chat Now

Ask the Community

Get help with your questions from our community of like-minded hosting users and Web Hosting Hub Staff.

Not a Customer?

Get web hosting from a company that is here to help.
}