Upload and Move a File


The Upload widget allows the user to upload a file to the RB root directory. If you want to store uploads in a separate directory, you can do it this way.

  1. Create a folder, perhaps called "uploads" with MkDir().
  2. Place an Upload widget on the page.
  3. If a file is uploaded, open it for binary access.
  4. Read the data into a string variable.
  5. Close the file.
  6. Open a file in your created "uploads" folder for binary access.
  7. Write the data from the string variable to the file.
  8. Close the file.
  9. Kill the original file.

The file has now been "moved" to the folder of your choice.

'Demo of uploading file and moving it to a
'different folder called uploads.
'The new folder is created in the program.
 
'create error handler
on error goto [oops]
 
'attempt to create a new folder called uploads
a = mkdir("uploads")
 
if a then
'if nonzero result, directory was created
print "Created ""uploads"" directory."
else
'if result=0, directory already exists
print "Directory ""uploads"" already exists."
end if
 
'add upload widget to page
upload "Select a file to upload."; file$
 
'filename is in file$, file is in root directory
'if no file uploaded, abort
if file$ = "" then end
 
'get contents of uploaded file into string variable
open file$ for binary as #f
filedata$ = input$(#f, LOF(#f))
close #f
 
'display a bit of filedata$ for test purposes
print "Filedata begins with ";left$(filedata$,20)
 
'open new file with same name in uploads folder
open "uploads\" + file$ for binary as #g
'write data to new file
print #g, filedata$;
close #g
 
print "File is now in uploads\" + file$
'delete uploaded file from root directory
kill file$
end
 
[oops]
print "The program has encountered an error."
print "Error number is ";Err;" and description is ";Err$
end