Raised: $0
0% of monthly goal Help us cross the finish line!
Goal: $12,000
Raised: $0 Goal: $12,000
0% of monthly goal Help us cross the finish line!
Sponsor DDEV

add-on registry tests last commit release

DDEV SFTP

Overview

This add-on integrates SFTP into your DDEV project.

Installation

ddev add-on get iljapolanskis/ddev-sftp
ddev restart

After installation, make sure to commit the .ddev directory to version control.

Usage

Command Description
ddev describe View service status and used ports for SFTP
ddev logs -s sftp Check SFTP logs

Configuration

You can customize the SFTP service by setting environment variables in your .ddev/.env file:

# SFTP port (host port mapping)
SFTP_PORT=2222

# SFTP credentials
SFTP_USERNAME=sftp
SFTP_PASSWORD=sftp

# SFTP directory and user/group IDs
SFTP_UID=1001
SFTP_GID=1001
SFTP_DIR=upload

# Host path to mount
SFTP_HOST_PATH=../upload/sftp

After making changes, restart DDEV:

ddev restart

Configuration Options

Variable Default Description
SFTP_PORT 2222 Host port for SFTP access
SFTP_USERNAME sftp SFTP username
SFTP_PASSWORD sftp SFTP password
SFTP_UID 1001 User ID for SFTP user
SFTP_GID 1001 Group ID for SFTP user
SFTP_DIR upload Directory name inside SFTP home
SFTP_HOST_PATH ../upload/sftp Host path to mount as SFTP directory

Connecting to SFTP

Once configured, you can connect to the SFTP service using:

PHP Usage Example

Here’s an example of how to use the SFTP service from within your PHP application:

First, install the required PHP library:

composer require phpseclib/phpseclib

Then use it in your code (playground.php file):

<?php

require 'vendor/autoload.php';

use phpseclib3\Net\SFTP;

$sftp = new SFTP('sftp');
if (!$sftp->login('sftp', 'sftp')) {
    die('Login Failed');
}

$content = "This is a test file.\nLine 2 of the file.\n";
$remoteFilePath = '/upload/test_file_' . time() . '.txt';
if ($sftp->put($remoteFilePath, $content)) {
    echo "File uploaded successfully to $remoteFilePath\n";
} else {
    echo "Failed to upload file to $remoteFilePath\n";
}

Note: This example uses the default SFTP service hostname (sftp) which is accessible from within the DDEV environment.

ddev exec php playground.php

Credits

Contributed and maintained by @iljapolanskis

This add-on uses the atmoz/sftp Docker image as the base for the SFTP service.