Click to See Complete Forum and Search --> : [RESOLVED] How to allow downloading of a file
dee-u
Aug 15th, 2009, 06:36 PM
I tried googling but perhaps I don't have the right keywords so I am asking here. As the title suggests, how can I allow a link in my site to download a file when it is clicked?
TIA
kows
Aug 16th, 2009, 04:48 AM
this?
<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
// The PDF source is in original.pdf
readfile('original.pdf');
?>
from the examples of header() (http://ca3.php.net/manual/en/function.header.php) on PHP.net.
dee-u
Aug 16th, 2009, 10:15 PM
Thanks, I will try that when I have more time to fiddle with it. I actualy have a application which I want to share to the public so I want it to be downloadable, it is an Exe so will there be any special modification that I have to make to that sample you gave?
kows
Aug 17th, 2009, 01:05 AM
change the files names, that's about it.
but if you're just giving people one file, then you could just link to the exe file itself?
dee-u
Aug 17th, 2009, 01:38 AM
How do I provide a link to the exe itself? Sorry but if its plain html then I have totally forgotten it being a desktop developer for years.
kows
Aug 17th, 2009, 02:20 PM
... are you kidding?
<a href="http://website.com/file.exe">download</a>
dee-u
Aug 17th, 2009, 02:45 PM
No I am not. =) I haven't really previously tried such so I didn't know. What would be the difference of that and the one that uses PHP as in your post #2?
SambaNeko
Aug 17th, 2009, 04:12 PM
If you link to a file format that a browser thinks it can open by itself (like .pdf, .txt, .html, .php, .jpg, etc.), then it will usually try to do so. If you want the browser not to try to open it, and instead prompt the user to save the file, then you use something like kow's post #2. But most browsers will not try to open an .exe, and will automatically go to the download prompt simply by linking to them.
dee-u
Aug 30th, 2009, 05:43 AM
... are you kidding?
<a href="http://website.com/file.exe">download</a>
Somehow this does not work to me. I only used
a href="file.exe
Using Opera upon clicking the link it does not continue, it just keep loading. Using IE it went to site/file.exe but does not prompt to download the file, why is this?
The file is 3MB if it makes any special treatment. I have even tried the script found here (http://www.zubrag.com/scripts/download.php) but I cannot make it work. Its giving me an error
Warning: opendir(/downloads/) [function.opendir]: failed to open dir: No such file or directory in /home/a9056016/public_html/download.php on line 95
and
Warning: readdir(): supplied argument is not a valid Directory resource in /home/a9056016/public_html/download.php on line 97
I will keep looking into this but I hope someone else more knowledgeable on this could pinpoint to me what I am doing wrong.
SambaNeko
Aug 30th, 2009, 09:48 AM
Somehow this does not work to me. I only used
a href="file.exe
What do you mean by that?... You need to use the whole snippet that kows gave: <a href="http://website.com/file.exe">download</a> ...
The errors you're encountering with the download script are probably because you haven't set it up properly - on line 22, see this line:
define('BASE_DIR','/home/user/downloads/');
This specifies the path to the file(s) you want to download. The errors would suggest that you've left this as the default value, and not customized it with your own path.
kows
Aug 30th, 2009, 11:35 AM
since you're on a linux server, if you want to make a relative file path use this format:
define('BASE_DIR','./downloads/');
otherwise, using a slash in the beginning will start looking at the root of your filesystem. if you do this, you'll need to include the full path to your web directory, which seems to be '/home/a9056016/public_html/', so your BASE_DIR might look like this if you don't use the method above:
define('BASE_DIR','/home/a9056016/public_html/downloads/');
dee-u
Aug 30th, 2009, 12:36 PM
since you're on a linux server, if you want to make a relative file path use this format:
define('BASE_DIR','./downloads/');
otherwise, using a slash in the beginning will start looking at the root of your filesystem. if you do this, you'll need to include the full path to your web directory, which seems to be '/home/a9056016/public_html/', so your BASE_DIR might look like this if you don't use the method above:
define('BASE_DIR','/home/a9056016/public_html/downloads/');
Cool, that did it, I did try '/downloads/' without the period. Thanks!
EDIT: Darn, got to spread first before I can rate you again. =(
dee-u
Aug 31st, 2009, 10:14 PM
Sorry, I am now able to offer a download but it seems that there is a problem when the file is being downloaded, it just stops in the middle of downloading, here is the script of the download code, could anyone point out what's wrong?
$file = @fopen($file_path,"rb");
if ($file) {
while(!feof($file)) {
set_time_limit(0);
print(fread($file, 1024*8));
flush();
if (connection_status()!=0) {
@fclose($file);
die();
}
}
@fclose($file);
}
I've even attempted to compress the file and it is being downloaded completely but when one tries to open the compressed file it says it is corrupted and cannot be extracted.
I hope someone here could shed light on this. Thanks!
penagate
Aug 31st, 2009, 10:41 PM
dee-u,
I did not quite follow why you have tried to use PHP to stream the file out rather than providing a direct link to it.
As long as your web server is configured to serve files with an '.exe' extension as application/octet-stream and with Content-disposition:attachment (which nearly all web servers are by default) then you will have no problems and you avoid having to invoke the PHP interpreter for a task which really does not concern it.
The only reason I can think of for streaming a file via PHP is to provide access control based on custom authentication logic. Even in this case I would advise that you try instead generating a temporary link and redirecting the client to that, rather than streaming through PHP.
dee-u
Aug 31st, 2009, 10:52 PM
Hmmmnnn... Thanks for pointing it out, I think I had a problem with a direct link for the exe, what I tried now is to zip the file and just used a direct link to the zipped exe, I'm still testing it if it will work.
Thanks!
dee-u
Aug 31st, 2009, 11:06 PM
The direct link to the zip file works without hassle.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.