Jump to content

Archived

This topic is now archived and is closed to further replies.

PC Nerd

downloads

Recommended Posts

i want to create a download link and i dont know how to do it.  i simply want to put a pyhon prgoram that i have written opn my web site.  Can i do this with html or php.  wats the code for it ???????

can any one help me.

all help is much appreciated

Share this post


Link to post
Share on other sites
You just upload the file to your web server and you have the download link, if you wanna display the link just do this:
[code]<a href="URL">words</a>[/code]

Replace the URL with the download link, and the words to any words (it could be the file name).

Share this post


Link to post
Share on other sites
You simply do what hackerkts said, or if you're talking about keeping the file off the public web directory (private area), then you want to force a download. Read this topic:

http://www.phpfreaks.com/forums/index.php/topic,97105.msg389027.html#msg389027

Share this post


Link to post
Share on other sites
if your  not using a database then do this ok

<a href="file_name.zip">donload my game</a>

Just remeber i am using a non database way and the file has to be a zip file and in the same folder as the
html code.

Share this post


Link to post
Share on other sites
thanks guys, so i simply link to the file, eg

<a href = "../downloads/program.py">Download</a>

this will force the user to dowload the file program.py


???

ill try it noew

thanks again

Share this post


Link to post
Share on other sites
to force downloads this is the correct method as posted link from toplay

<?PHP
ob_start();
$fname = 'http://www.example.com/test.txt';

header('HTTP/1.1 200 OK');
header('Content-Type: application/unknown');
header('Content-Disposition: attachment; filename=' . basename($fname));
header('Content-Transfer-Encoding: binary');

readfile($fname);

header('Content-Length: ' . ob_get_length());

ob_end_flush();

echo 'This will not show in browser or be in file';
?>

Share this post


Link to post
Share on other sites
ok thanks.  can you walk me through wat the highlighted code does.  I dont quite understand wat some of the code does.  if you could that would be great

<quote>

<?PHP
[color=red]ob_start();[/color]
$fname = 'http://www.example.com/test.txt';

[color=red]header('HTTP/1.1 200 OK');
header('Content-Type: application/unknown');
header('Content-Disposition: attachment; filename=' . basename($fname));
header('Content-Transfer-Encoding: binary');

readfile($fname);

header('Content-Length: ' . ob_get_length());

ob_end_flush();[/color]
echo 'This will not show in browser or be in file';
?>

</quote>

i know it look really bad, but i always learn how to script things myself, not just copy others work.

Share this post


Link to post
Share on other sites
just look up header php ok then lookup the code you dont understand ok.


research research lol................................

trust me your get the code more inbeded ok

good luck.

Share this post


Link to post
Share on other sites
[code]ob_start();[/code]
Turns on 'Output buffering', meaning everything you intend to output, will be captured in an internal buffer instead of being directly outputted.

[code]header('HTTP/1.1 200 OK');
header('Content-Type: application/unknown');
header('Content-Disposition: attachment; filename=' . basename($fname));
header('Content-Transfer-Encoding: binary');[/code]
These are some HTTP/1.1 Protocol headers, you can read up on the HTTP/1.1 protcol here: [url=http://www.w3.org/Protocols/rfc2616/rfc2616.html]http://www.w3.org/Protocols/rfc2616/rfc2616.html[/url]

[code]readfile($fname);[/code]
Is a PHP function that reads a file directly into your script, binary-safe. In other words the contents of that file will be placed into your script.

[code]header('Content-Length: ' . ob_get_length());[/code]
This is another HTTP header, your sending over the length of your document, ob_get_length() returns the size of the internal output buffer in bytes.

[code]ob_end_flush();[/code]
This outputs what is currently in the internal buffer then turns off output buffering.

HTH, Zac.




Share this post


Link to post
Share on other sites
ok so how does this get turned into the dowload.  i am reaonably new to php and dont quite get where the link to the file goes.

thanks for your help

Pc Nerd

Share this post


Link to post
Share on other sites
[quote author=PC Nerd link=topic=99144.msg390338#msg390338 date=1151813242]
ok so how does this get turned into the dowload.  i am reaonably new to php and dont quite get where the link to the file goes.

thanks for your help

Pc Nerd
[/quote]
When a user clicks on the link you did, then it will download the file. It's the simplest approach.

If you want to force a download to protect file location etc. then use code similar to what's provided. Please click on the link to the topic I provided and read it. It describes an approach (links.php & download.php). There's a link to a HTTP protocol book recommendation there too.

If you have the file saved locally, then use filesize() function to populate the Content-Length header and all the buffering stuff is not needed. It was used in that topic because the person had the file remotely using a full URL where filesize() doesn't work.

Good luck.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.