|
-
Aug 15th, 2009, 06:36 PM
#1
[RESOLVED] How to allow downloading of a file
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
Last edited by dee-u; Aug 31st, 2009 at 10:09 PM.
-
Aug 16th, 2009, 04:48 AM
#2
Re: How to allow downloading of a file
this?
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'); ?>
from the examples of header() on PHP.net.
-
Aug 16th, 2009, 10:15 PM
#3
Re: How to allow downloading of a file
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?
-
Aug 17th, 2009, 01:05 AM
#4
Re: How to allow downloading of a file
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?
-
Aug 17th, 2009, 01:38 AM
#5
Re: How to allow downloading of a file
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.
-
Aug 17th, 2009, 02:20 PM
#6
Re: How to allow downloading of a file
... are you kidding?
Code:
<a href="http://website.com/file.exe">download</a>
-
Aug 17th, 2009, 02:45 PM
#7
Re: How to allow downloading of a file
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?
-
Aug 17th, 2009, 04:12 PM
#8
Re: How to allow downloading of a file
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.
-
Aug 30th, 2009, 05:43 AM
#9
Re: How to allow downloading of a file
 Originally Posted by kows
... are you kidding?
Code:
<a href="http://website.com/file.exe">download</a>
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?
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
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.
-
Aug 30th, 2009, 09:48 AM
#10
Re: How to allow downloading of a file
 Originally Posted by dee-u
Somehow this does not work to me. I only used
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:
Code:
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.
-
Aug 30th, 2009, 11:35 AM
#11
Re: How to allow downloading of a file
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/');
-
Aug 30th, 2009, 12:36 PM
#12
Re: How to allow downloading of a file
 Originally Posted by kows
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. =(
-
Aug 31st, 2009, 10:14 PM
#13
Re: How to allow downloading of a file
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?
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'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!
-
Aug 31st, 2009, 10:41 PM
#14
Re: How to allow downloading of a file
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.
-
Aug 31st, 2009, 10:52 PM
#15
Re: How to allow downloading of a file
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!
-
Aug 31st, 2009, 11:06 PM
#16
Re: [RESOLVED] How to allow downloading of a file
The direct link to the zip file works without hassle.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|