PC Nerd Posted July 2, 2006 Share Posted July 2, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/13419-downloads/ Share on other sites More sharing options...
hackerkts Posted July 2, 2006 Share Posted July 2, 2006 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). Quote Link to comment https://forums.phpfreaks.com/topic/13419-downloads/#findComment-51806 Share on other sites More sharing options...
toplay Posted July 2, 2006 Share Posted July 2, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/13419-downloads/#findComment-51811 Share on other sites More sharing options...
redarrow Posted July 2, 2006 Share Posted July 2, 2006 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 thehtml code. Quote Link to comment https://forums.phpfreaks.com/topic/13419-downloads/#findComment-51826 Share on other sites More sharing options...
PC Nerd Posted July 2, 2006 Author Share Posted July 2, 2006 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 noewthanks again Quote Link to comment https://forums.phpfreaks.com/topic/13419-downloads/#findComment-51831 Share on other sites More sharing options...
redarrow Posted July 2, 2006 Share Posted July 2, 2006 to force downloads this is the correct method as posted link from toplay <?PHPob_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';?> Quote Link to comment https://forums.phpfreaks.com/topic/13419-downloads/#findComment-51833 Share on other sites More sharing options...
PC Nerd Posted July 2, 2006 Author Share Posted July 2, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/13419-downloads/#findComment-51840 Share on other sites More sharing options...
redarrow Posted July 2, 2006 Share Posted July 2, 2006 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 okgood luck. Quote Link to comment https://forums.phpfreaks.com/topic/13419-downloads/#findComment-51844 Share on other sites More sharing options...
heckenschutze Posted July 2, 2006 Share Posted July 2, 2006 [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. Quote Link to comment https://forums.phpfreaks.com/topic/13419-downloads/#findComment-51847 Share on other sites More sharing options...
PC Nerd Posted July 2, 2006 Author Share Posted July 2, 2006 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 helpPc Nerd Quote Link to comment https://forums.phpfreaks.com/topic/13419-downloads/#findComment-51851 Share on other sites More sharing options...
toplay Posted July 2, 2006 Share Posted July 2, 2006 [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 helpPc 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. Quote Link to comment https://forums.phpfreaks.com/topic/13419-downloads/#findComment-51992 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.