This add-on integrates SFTP into your DDEV project.
ddev add-on get iljapolanskis/ddev-sftp
ddev restartAfter installation, make sure to commit the .ddev directory to version control.
| Command | Description |
|---|---|
ddev describe |
View service status and used ports for SFTP |
ddev logs -s sftp |
Check SFTP logs |
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/sftpAfter making changes, restart DDEV:
ddev restart| 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 |
Once configured, you can connect to the SFTP service using:
- Host:
localhost(or your DDEV hostname) - Port: Value of
SFTP_PORT(default: 2222) - Username: Value of
SFTP_USERNAME(default: sftp) - Password: Value of
SFTP_PASSWORD(default: sftp)
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/phpseclibThen 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.phpContributed and maintained by @iljapolanskis
This add-on uses the atmoz/sftp Docker image as the base for the SFTP service.