This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Appleeater's profile picture

Download File From Webserver Using HTTP.API

Started by Appleeater, 17 May 2013 - 10:55 AM
Appleeater #1
Posted 17 May 2013 - 12:55 PM
Hey everyone, I am currently trying to update my API to incorpararate a Auto-Updater. I want to know: How can I download a file from my own server (not pastebin of GitHub).

My current Code:


local version = 1.0
local ckeckForUpdate = http.get("http://www.example.com/currentVersion.php")
local checkForUpdate = checkForUpdate.readAll()
if not version == checkForUpdate then
--Here comes the download from server
else
--API HERE
end



How could I achieve this?
LBPHacker #2
Posted 17 May 2013 - 01:08 PM
Almost. But closing the HTTP handle is a wise thing and another one is not having multiple variable with the same name.
local httpHandle = http.get("whatever.com")
local netVersion = httpHandle.readAll()
httpHandle.close()
-- process netVersion here

EDIT: If the version you're checking for is a number, you should tonumber netVersion. I prefer storing the version number in a string though.
Sammich Lord #3
Posted 17 May 2013 - 01:10 PM
You would need a PHP script to list the files inside the directory of the files to download. Then you would need a loop to download each file.
Here is the PHP code for it: http://php.net/manua...ion.readdir.php
Then this would be some example Lua code to loop through it and download it:

local downloadPath = 'http://site.tld/downloads/v1/'
local files = http.get('http://site.tld/downloads/list.php')
local line = files.readLine()
while line do
  local req = http.get(downloadPath..line)
  local f = fs.open(line, 'w')
  f.write(req.readAll())
  red.close()
  f.close()
  line = files.readLine()
end
files.close()
H4X0RZ #4
Posted 17 May 2013 - 05:46 PM
You would need a PHP script to list the files inside the directory of the files to download. Then you would need a loop to download each file.
Here is the PHP code for it: http://php.net/manua...ion.readdir.php
Then this would be some example Lua code to loop through it and download it:

local downloadPath = 'http://site.tld/downloads/v1/'
local files = http.get('http://site.tld/downloads/list.php')
local line = files.readLine()
while line do
  local req = http.get(downloadPath..line)
  local f = fs.open(line, 'w')
  f.write(req.readAll())
  red.close()
  f.close()
  line = files.readLine()
end
files.close()

There is little typo inline eight. You have written red instead of req ;)/>
Sammich Lord #5
Posted 17 May 2013 - 05:55 PM
You would need a PHP script to list the files inside the directory of the files to download. Then you would need a loop to download each file.
Here is the PHP code for it: http://php.net/manua...ion.readdir.php
Then this would be some example Lua code to loop through it and download it:

local downloadPath = 'http://site.tld/downloads/v1/'
local files = http.get('http://site.tld/downloads/list.php')
local line = files.readLine()
while line do
  local req = http.get(downloadPath..line)
  local f = fs.open(line, 'w')
  f.write(req.readAll())
  red.close()
  f.close()
  line = files.readLine()
end
files.close()

There is little typo inline eight. You have written red instead of req ;)/>
I wrote it without testing :P/> But the concept works :P/> I never intend on people using my example code. I assume they will just use it as a guide for their own code.
tuxarn #6
Posted 24 September 2013 - 01:44 PM
You would need a PHP script to list the files inside the directory of the files to download. Then you would need a loop to download each file.
Here is the PHP code for it: http://php.net/manua...ion.readdir.php
Then this would be some example Lua code to loop through it and download it:

local downloadPath = 'http://site.tld/downloads/v1/'
local files = http.get('http://site.tld/downloads/list.php')
local line = files.readLine()
while line do
  local req = http.get(downloadPath..line)
  local f = fs.open(line, 'w')
  f.write(req.readAll())
  red.close()
  f.close()
  line = files.readLine()
end
files.close()

Hello, First post here! :D/>

question: I tested this out for my server to be able to store a database of useful programs for myself mainly but others on the server as well but i would like it to download other programs that are more "testingstuff" to a different directory, now all the stuff is going to the root so how do i change that, using your code?
I have made two separate scripts that access two different folders on my webserver using two different list scripts with php. so this is the only missing part i dont get, i have checked the fs api for clues but with no luck. I just started programming three days ago i might add, so go easy on me :)/> Thanks in advance!
Lyqyd #7
Posted 24 September 2013 - 02:04 PM
This topic has been dead for quite a while, so you may not get the answer you're looking for. Why don't you go read the sticky posts to find where to post your own question as a new member, and post your whole code and ask for help doing what you're looking to accomplish? You'd probably get better help that way.