FTP

The FTP module allows you perform various operations against an FTP or SFTP server.

📁 A comprehensive FTP client module for BoxLang that brings seamless file transfer capabilities to your applications!

This module provides powerful FTP client functionality to the BoxLang language, making it easy to connect to FTP servers, transfer files, and manage remote directories with minimal code.

✨ Features

  • 🌐 FTP/FTPS Support: Connect to standard FTP and secure FTP servers

  • 📁 Complete File Operations: Upload, download, rename, and delete files

  • 📂 Directory Management: Create, remove, list, and navigate directories

  • 🔐 Secure Connections: Support for passive and active modes

  • 🎯 Connection Pooling: Named connections tracked globally via FTPService

  • 🔄 Proxy Support: Connect through proxy servers with custom ports

  • 📊 Flexible Results: Query or array return types for directory listings

  • Event-Driven: Interception points for logging, monitoring, and custom logic

  • 🛠️ Production Ready: Built by Ortus Solutions with enterprise-grade quality

  • 🔧 Zero Configuration: Sensible defaults get you started quickly

📦 Installation

Requirements

  • BoxLang: 1+

  • Java: JDK 21+

Install via CommandBox CLI

If you are using CommandBox for your web applications, you can install it via CommandBox:

Install via BoxLang Binary

If you want to install it globally for the BoxLang OS binary, use the install-bx-module command:

The module will automatically register and be available as bx:ftp or <bx:ftp> in your BoxLang script and template applications.

From Source

🚀 Quick Start

Here's how to connect to an FTP server and upload a file in just a few lines:

That's it! 🎉 You now have a fully functional FTP client.

💡 Usage Examples

Basic Examples

📁 Connect and List Files

📤 Upload a File

📥 Download a File

Advanced Examples

🔐 Secure Connection with Passive Mode

🌐 Connect Through Proxy Server

📂 Directory Management

📊 List Directory as Array

🔄 Bulk File Transfer

🏷️ Rename Files and Directories

📚 Available Actions

All actions can use a result attribute to store the result of the action in a variable. If not provided, the result will be stored in a variable called bxftp (or cftp if you are in CFML compat mode).

🔌 Connection Actions

open - Connect to FTP Server

Opens a connection to an FTP server and tracks it in the FTPService.

Attributes:

Attribute
Type
Required
Default
Description

connection

string

✅ Yes

-

Name of the connection to track

server

string

✅ Yes

-

Server IP or hostname

port

numeric

No

21

FTP port number

username

string

✅ Yes

-

Authentication username

password

string

✅ Yes

-

Authentication password

timeout

numeric

No

30

Connection timeout in seconds

secure

boolean

No

false

Use secure FTP (FTPS)

passive

boolean

No

true

Use passive mode

proxyServer

string

No

-

Proxy server (hostname:port)

Examples:

close - Close FTP Connection

Closes an open FTP connection and removes it from the FTPService.

Attributes:

Attribute
Type
Required
Description

connection

string

✅ Yes

Name of the connection to close

Example:

📂 Directory Actions

changedir - Change Working Directory

Changes the current working directory on the FTP server.

Attributes:

Attribute
Type
Required
Description

connection

string

✅ Yes

Connection name

directory

string

✅ Yes

Directory path to change to

Example:

createdir - Create Directory

Creates a new directory on the FTP server.

Attributes:

Attribute
Type
Required
Description

connection

string

✅ Yes

Connection name

new

string

✅ Yes

Path of directory to create

Example:

existsdir - Check Directory Existence

Checks if a directory exists on the FTP server.

Attributes:

Attribute
Type
Required
Description

connection

string

✅ Yes

Connection name

directory

string

✅ Yes

Directory path to check

Example:

getcurrentdir - Get Working Directory

Retrieves the current working directory path.

Attributes:

Attribute
Type
Required
Description

connection

string

✅ Yes

Connection name

Example:

listdir - List Directory Contents

Lists files and directories in the specified directory.

Attributes:

Attribute
Type
Required
Default
Description

connection

string

✅ Yes

-

Connection name

directory

string

✅ Yes

-

Directory to list

name

string

✅ Yes

-

Variable name to store results

returnType

string

No

"query"

Return format: "query" or "array"

Query Columns:

  • name - File/directory name

  • isDirectory - Boolean indicating if item is a directory

  • lastModified - Last modification timestamp

  • size - File size in bytes (aliased as length for CFML compatibility)

  • mode - File permissions mode

  • path - File path without drive designation

  • url - Complete URL for the item

  • type - Type: "file", "directory", "symbolic link", or "unknown"

  • raw - Raw FTP listing representation

  • attributes - File attributes

  • isReadable - Boolean for read permission

  • isWritable - Boolean for write permission

  • isExecutable - Boolean for execute permission

Examples:

removedir - Remove Directory

Removes a directory from the FTP server.

Attributes:

Attribute
Type
Required
Description

connection

string

✅ Yes

Connection name

directory

string

✅ Yes

Directory path to remove

Example:

renamedir - Rename Directory

Renames a directory on the FTP server.

Attributes:

Attribute
Type
Required
Description

connection

string

✅ Yes

Connection name

existing

string

✅ Yes

Current directory path

new

string

✅ Yes

New directory path

Example:

📄 File Actions

existsfile - Check File Existence

Checks if a file exists on the FTP server.

Attributes:

Attribute
Type
Required
Description

connection

string

✅ Yes

Connection name

remoteFile

string

✅ Yes

Remote file path to check

Example:

getfile - Download File

Downloads a file from the FTP server to the local filesystem.

Attributes:

Attribute
Type
Required
Default
Description

connection

string

✅ Yes

-

Connection name

remoteFile

string

✅ Yes

-

Remote file path to download

localFile

string

✅ Yes

-

Local file path to save to

failIfExists

boolean

No

true

Fail if local file already exists

Example:

putfile - Upload File

Uploads a file from the local filesystem to the FTP server.

Attributes:

Attribute
Type
Required
Description

connection

string

✅ Yes

Connection name

localFile

string

✅ Yes

Local file path to upload

remoteFile

string

✅ Yes

Remote file path destination

Example:

removefile (or remove) - Delete File

Deletes a file from the FTP server.

Attributes:

Attribute
Type
Required
Description

connection

string

✅ Yes

Connection name

remoteFile

string

✅ Yes

Remote file path to delete

Example:

renamefile - Rename File

Renames a file on the FTP server.

Attributes:

Attribute
Type
Required
Description

connection

string

✅ Yes

Connection name

existing

string

✅ Yes

Current file path

new

string

✅ Yes

New file path

Example:

📊 Result Object

All FTP actions return a result object with the following structure:

Common Status Codes

Code
Meaning
Description

200

Success

Command successful

221

Goodbye

Service closing control connection

226

Transfer complete

Closing data connection, file transfer successful

230

Login successful

User logged in

250

Success

Requested file action okay, completed

550

Failed

File unavailable or permission denied

552

Exceeded

Storage allocation exceeded

553

Not allowed

File name not allowed

Using Result Variables

🎯 Interception Points

The FTP module announces several interception points that allow you to hook into the FTP operation lifecycle for logging, monitoring, metrics, or custom logic.

beforeFTPCall

Announced before any FTP action is executed.

Interceptor Data:

Use Cases:

  • Pre-flight validation

  • Logging all FTP operations

  • Performance monitoring (start timer)

  • Security auditing

Example:

afterFTPCall

Announced after an FTP action completes successfully.

Interceptor Data:

Use Cases:

  • Success logging

  • Performance metrics (calculate duration)

  • Notifications or alerts

  • Analytics and reporting

Example:

onFTPConnectionOpen

Announced when a new FTP connection is opened.

Interceptor Data:

Use Cases:

  • Connection logging

  • Connection pooling metrics

  • Security monitoring

  • Access auditing

Example:

onFTPConnectionClose

Announced when an FTP connection is closed.

Interceptor Data:

Use Cases:

  • Connection cleanup logging

  • Session duration tracking

  • Connection pool management

  • Resource monitoring

Example:

onFTPError

Announced when an FTP operation encounters an error.

Interceptor Data:

Use Cases:

  • Error logging and alerting

  • Retry logic

  • Fallback mechanisms

  • Error analytics

Example:

🔗 Connection Management

The FTP module uses the FTPService to manage named connections globally across your application.

Connection Lifecycle

  1. Open: Connection is created and tracked by name

  2. Use: Multiple actions can use the same connection

  3. Close: Connection is removed from tracking

Reusing Connections

Connection Best Practices

  • Always close connections when done to free resources

  • Use descriptive names for connections to avoid conflicts

  • Reuse connections for multiple operations to improve performance

  • Use try/finally to ensure connections are closed even on errors

  • Don't leave connections open indefinitely

Accessing Connection Information

After opening a connection, it's stored in a variable with the connection name:

❌ Error Handling

Common Connection Issues

Connection Refused Errors:

  • Verify FTP server is running and accessible

  • Check firewall settings for FTP ports (21 for control, 20 for data)

  • For passive mode, ensure passive port range is accessible

  • Verify credentials are correct

Passive vs Active Mode:

  • Passive Mode (default): Client initiates both control and data connections

  • Active Mode: Server initiates data connection back to client

  • Use passive mode for connections through firewalls/NAT

Error Handling Patterns

Try/Catch/Finally

Status Code Checking

❓ Troubleshooting

Connection Refused Errors

Problem: FTP operations fail with "Connection refused" errors.

Solutions:

  • ✅ Verify FTP server is running: nc -v hostname port

  • ✅ Check firewall allows FTP ports (21, 20, and passive range)

  • ✅ Ensure passive mode is configured on server (for passive=true)

  • ✅ Verify credentials are correct

  • ✅ Test connection with FTP client (FileZilla, etc.)

Passive Mode Failures

Problem: Connection succeeds but file transfers fail.

Solutions:

  • ✅ Ensure FTP server has passive mode enabled

  • ✅ Check passive port range is open in firewall

  • ✅ Verify server announces correct IP for passive connections

  • ✅ Try active mode instead: passive="false"

File Upload Failures

Problem: Files fail to upload with permission errors.

Solutions:

  • ✅ Verify user has write permissions on remote directory

  • ✅ Check disk space on FTP server

  • ✅ Ensure remote directory exists

  • ✅ Try uploading to a different directory

Connection Timeout

Problem: Connection attempts timeout.

Solutions:

  • ✅ Increase timeout: timeout="60"

  • ✅ Check network connectivity to FTP server

  • ✅ Verify server is not blocking your IP

  • ✅ Test if server is behind a firewall or proxy

Proxy Connection Issues

Problem: Cannot connect through proxy server.

Solutions:

  • ✅ Verify proxy server address and port: proxyServer="proxy.company.com:8080"

  • ✅ Check proxy supports FTP protocol

  • ✅ Ensure proxy authentication (if required) is configured

  • ✅ Test proxy with other FTP clients

🛠️ Development & Testing

Local Development Setup

This module includes Docker configuration for local FTP server testing:

Test Server Configuration

  • Host: localhost

  • Control Port: 2221

  • Data Port: 2220

  • Passive Ports: 10000-10010

  • Username: test_user

  • Password: testpass

Building from Source

Module Architecture

  • FTPService: Global singleton service managing all FTP connections

  • FTP Component: BoxLang component (@BoxComponent) providing FTP operations

  • FTPConnection: Represents an active FTP connection with state management

  • FTPResult: Encapsulates operation results with status codes and messages

  • Dependencies: Apache Commons Net 3.11.1 for FTP protocol implementation

Testing with Docker

The included Docker setup provides a consistent FTP testing environment:

Important: The vsftpd.conf file includes critical passive mode configuration:

🔄 CFML Compatibility

This module will require the bx-cfml-compat module if you want it to work like Adobe ColdFusion/Lucee in your CFML applications.

Differences from CFML

  • Result Variable: BoxLang uses bxftp by default, CFML compat mode uses cftp

  • Component Name: Use bx:ftp in BoxLang, cftp with compat module

  • Features: BoxLang FTP includes modern features like event interception

Migration from CFML

All CFML <cfftp> should work with no changes when using bx-cfml-compat. However, for native BoxLang usage, update tags as follows:

🤝 Contributing

We ❤️ contributions! This project is open source and welcomes your help to make it even better.

🐛 Found a Bug?

If you discover a bug, please:

  1. Check existing issues at GitHub Issues

  2. Create a new issue with:

    • Clear title and description

    • Steps to reproduce

    • Expected vs actual behavior

    • BoxLang version and environment details

    • Sample code that demonstrates the issue

💡 Have an Enhancement Idea?

We'd love to hear your ideas! Please:

  1. Describe the feature and its use case

  2. Explain how it would benefit users

📚 Improve Documentation

Documentation improvements are always welcome:

  • Fix typos or unclear explanations

  • Add more examples

  • Improve code comments

  • Create tutorials or guides

💰 Financial Support

You can support BoxLang and all Ortus Solutions open source projects:

Patrons get exclusive benefits like:

  • Priority support

  • Early access to new features

  • FORGEBOX Pro account

  • CFCasts account

📞 Support Channels

Need help? Don't create an issue—use our support channels:

🏆 Contributors

Thank you to all our amazing contributors! ❤️

Contributors

Made with contributors-img

🔐 Security Vulnerabilities

If you discover a security vulnerability:

  1. DO NOT create a public issue

  2. Report in #security channel on Box Team Slack

All vulnerabilities will be promptly addressed.

📄 License

This project is licensed under the Apache License 2.0.

See LICENSE file for full details.

💼 Support & Resources

📖 Documentation

🎓 Learning Resources

THE DAILY BREAD

"I am the way, and the truth, and the life; no one comes to the Father, but by me (JESUS)" Jn 14:1-12


Copyright Since 2025 by Ortus Solutions, Corp www.boxlang.io | www.ortussolutions.com

Last updated

Was this helpful?