How to download a file from the web in ScriptFTP



ScriptFTP has commands to retrieve files from FTP sites only but it can also download files from the web (HTTP/HTTPS) using an external tool like CURL. You can download it going to this page. The Windows version of CURL is almost at the bottom of that page.
Once you have downloaded it you need to place curl.exe in any folder and call it from ScriptFTP using the EXEC command this way:
# Set the current local directory
LOCALCHDIR("C:\Users\Carlos\Desktop\curl_downloaded_files")
 
# Call curl
EXEC("C:\path_to\curl\curl.exe -O https://www.mydomain.com/thefile.zip")
If you browse the CURL website or run “curl.exe –help” you will notice the big amount of options and switches it has. It is a very versatile tool that not only downloads file from websites, it is also capable of automating form filling, HTTP authentication etc. For example, if the web browser prompts to enter an user name and password you can tell curl to login this way:
# Download a file using HTTP authentication
EXEC("C:\path_to\curl\curl.exe --user johndoe:thepswd -O https://www.mydomain.com/thefile.zip")
Note that HTTP authentication is not the kind of authentication you commonly find when going to web sites like yahoo mail, gmail etc. These are just HTML forms. You can identify HTTP authentication when the browser asks for the login in a very spartan pop-up window on top of the web browser.

Let’s see a complete script that downloads a file using curl and uploads the file to a FTP folder.
# Set the current local directory
LOCALCHDIR("C:\Users\Carlos\Desktop\curl_downloaded_files")
 
# Download the file
EXEC("C:\path_to\curl\curl.exe -O https://www.mydomain.com/thefile.zip")
 
# Connect to the FTP site
OPENHOST("ftp.myhost.com","myuser","123456")
 
# Upload the file
PUTFILE("thefile.zip")
 
# Close the connection
CLOSEHOST
We can also add some error handling to the script. In the case CURL fails downloading the file the script will stop
# Set the current local directory
LOCALCHDIR("C:\Users\Carlos\Desktop\curl_downloaded_files")
 
# Download the file
$result=EXEC("C:\path_to\curl\curl.exe -O https://www.mydomain.com/thefile.zip")
IF($result!="0")
   STOP
END IF
 
# Connect to the FTP site
OPENHOST("ftp.myhost.com","myuser","123456")
 
# Upload the file
PUTFILE("thefile.zip")
 
# Close the connection
CLOSEHOST
If you search for tools like CURL you will find another popular one called WGET. CURL is way more powerful but WGET is simpler to use and also can download entire web sites. It can also be used from ScriptFTP just the same way, calling the EXEC command.