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
Printable View
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
this?
from the examples of header() on PHP.net.PHP Code:<?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');
?>
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?
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?
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.
... are you kidding?
Code:<a href="http://website.com/file.exe">download</a>
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?
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.
Somehow this does not work to me. I only used
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?Code:a href="file.exe
The file is 3MB if it makes any special treatment. I have even tried the script found here but I cannot make it work. Its giving me an error
andQuote:
Warning: opendir(/downloads/) [function.opendir]: failed to open dir: No such file or directory in /home/a9056016/public_html/download.php on line 95
I will keep looking into this but I hope someone else more knowledgeable on this could pinpoint to me what I am doing wrong.Quote:
Warning: readdir(): supplied argument is not a valid Directory resource in /home/a9056016/public_html/download.php on line 97
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:
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.Code:define('BASE_DIR','/home/user/downloads/');
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/');
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?
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.Code:$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 hope someone here could shed light on this. Thanks!
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.
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!
The direct link to the zip file works without hassle.