Uploading a file using FTP

This lesson shows you how to use the libURL commands to upload a file via FTP to a web server.

(Optional) Create FTP Account

If you happen to have an on-rev account, you can create an account able to upload to a particular directory within your webspace with separate credentials from your regular account.

First, from cPanel, go to FTP Accounts.

In the next screen, fill in the credentials for the new account then click on "Create FTP Account".

Returning to the Tools page, go to File Manager.

From there, you may find the directory specified for the new FTP account (1). If not, use the Folder button to create it (2).

Create a stack with a button and field

Create a stack with a button and field

We start by creating a stack and dragging on a button for our script and a field to display our progress and results.

Select a file for upload

We start by selecting a local file that we want to upload. This is done using the 'answer file' command.

on mouseUp
	# Start by getting the file to upload
	local tFileForUpload
	answer file "Select a file to upload"
	put it into tFileForUpload
end mouseUp

Extract the file name

The second step is the extract the name of the file we wish to upload from the full path. We do this using the itemdelimiter property.

on mouseUp
	# Start by getting the file to upload
	local tFileForUpload
	answer file "Select a file to upload"
	put it into tFileForUpload
	
	# Get the name of the file for upload
	local tFileName
	set the itemdel to "/"
	put URLEncode(the last item of tFileForUpload) into tFileName
end mouseUp

Setup the FTP connection string

In this example we are going to use the libURLftpUploadFile command. It takes the following 3 parameters:

1) Path the the file you want to upload

example: /filepath/test.txt

2) FTP connection string with destination file path

structure: ftp://username:password@host/filepath

3) A callback handler

This is a handler in your script that will be called with a status message when libURL has finished.

example: uploadComplete

While this does sound a little complicated it is quite easy when you see the final code:

constant FTPHOST = "ftp.lctesting.livecodehosting.com"
constant FTPUSER = "[email protected]"
constant FTPPASS = "accountdeleted111"
on mouseUp
   local tUser
   put urlEncode(FTPUSER) into tUser
   
   # Start by getting the file to upload
   local tFileForUpload, tFileName
   answer file "Select a file to upload"
   put it into tFileForUpload
   	
   # Get the name of the file for upload
   set the itemdel to "/"
   put URLEncode(the last item of tFileForUpload) into tFileName
   put empty into field 1
   	
   # Connect the start the upload
   local tDestination
   put "ftp://" & tUser & ":" & FTPPASS & "@" & FTPHOST & "/" & tFileName into tDestination
   libURLftpUploadFile tFileForUpload, tDestination, "uploadComplete"
end mouseUp
on uploadComplete pURL, pStatus
   put "Status Update:" && pStatus &&  return before field 1
end uploadComplete

I have defined my connection information at the top of my script in constants. I use them to build the required connection string.

Add Progress

Add Progress

The final step is to add progress updates to your upload. When uploading large files you will want to know how the upload is going. Set the libURLSetStatusCallback before staring your upload to receive messages during upload.

1) You can see the progress message showing how much of the total file has been uploaded which you could use to create a nice visual progress bar.

Here is the final script:

constant FTPHOST = "ftp.lctesting.livecodehosting.com"
constant FTPUSER = "[email protected]"
constant FTPPASS = "accountdeleted111"
on mouseUp
   local tUser
   put urlEncode(FTPUSER) into tUser
   
   # Start by getting the file to upload
   local tFileForUpload, tFileName
   answer file "Select a file to upload"
   put it into tFileForUpload
   	
   # Get the name of the file for upload
   set the itemdel to "/"
   put URLEncode(the last item of tFileForUpload) into tFileName
   put empty into field 1
   	
   # Connect the start the upload
   local tDestination
   put "ftp://" & tUser & ":" & FTPPASS & "@" & FTPHOST & "/" & tFileName into tDestination
   libURLSetStatusCallback "uploadProgress", the long ID of me
   libURLftpUploadFile tFileForUpload, tDestination, "uploadComplete"
end mouseUp
on uploadComplete pURL, pStatus
   put "Status Update:" && pStatus &&  return before field 1
end uploadComplete
on uploadProgress pURL, pStatus
put "Status Update:" && pStatus &&  return before field 1
end uploadProgress

5 Comments

M├ędard

Thanks!
That works nicely :-)

Tim

Excellent, thank you so much, I have wanted to be able to do this for so long, but just did not have the understanding necessary to complete this basic programming.

This format of lessons on Run Rev is a fantastic Idea.

Nadesj

Nice, only very basic, and I am no where near completing this, to see if a file already exists and to ask if it is ok to overwrite the existing file, and how to do it when you want to upload folders?

But a nice starting point.

Jerry

Can I use this code with Android?

I need make a app to upload a file to ftp server every day at same hour.

Can someone help me?

Hanson Schmidt-Cornelius

Hi Jerry,

have a look at the dictionary entries for the particular syntax you want to use. You will find symbols against the particular dictionary entry that indicate on what platform the syntax is supported. "libURLftpUploadFile" is only supported on desktop machines.

Have a look at the "ftp" keyword, which is supported on mobile platforms too.

Kind Regards,

Hanson

Add your comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.