Open menu

Learn

SFTP File Uploading with PHP: A Comprehensive Guide

Secure File Transfer Protocol (SFTP) is a standard network protocol used for securely transferring files over a network. In this guide, you will learn how to perform SFTP file uploads using PHP with the phpseclib package.

Installing phpseclib

To connect to an SFTP server and upload files, we will be using the phpseclib package. This is a popular and reliable PHP library for working with various secure network protocols, including SFTP. It provides a pure PHP implementation of multiple protocols, including SFTP. Before proceeding with this tutorial, make sure you have installed the phpseclib package using Composer:
composer require phpseclib/phpseclib:~3.0

Getting Started with SFTP in phpseclib

To start with, you need to use the SFTP class provided by phpseclib . Here's an example of how to connect to an SFTP server:
require_once('vendor/autoload.php');
use phpseclib3\Net\SFTP;

// The credentials of the SFTP server, replace with your own
$host = 'eu-central-1.sftpcloud.io';
$user = 'my-sftp-user';
$password = 'my-sftp-password';

// Establish the connection
$sftp = new SFTP($host);
$sftp->login($user, $password);

Uploading Files

The put method provided by the SFTP class is used to upload files to an SFTP server. Depending on your needs, you can upload data as a string or a file.
// Upload data as a string
$sftp->put('filename.remote', 'xxx');

// Upload a local file
$sftp->put('filename.remote', 'filename.local', SFTP::SOURCE_LOCAL_FILE);
In the first put call, the method creates a file named filename.remote on the SFTP server and fills it with the string 'xxx'. In the second call, the method creates a file named filename.remote on the server and fills it with the contents of the local file filename.local

Resuming Transfers

Transfers might sometimes be interrupted, but phpseclib provides a way to resume them:
$sftp->put('filename.remote', 'filename.local', SFTP::SOURCE_LOCAL_FILE | SFTP::RESUME);
In this case, the put function appends all but the first $sftp->size('filename.remote') bytes of filename.local to filename.remote . This means that if your transfer is interrupted, you can restart it.

Positional Control

phpseclib's put function allows for positional control over the data transfer with the $start and $local_start parameters. These parameters let you control where in the file to start writing, giving you the power to write at the end of a file or in the middle of one.
// Upload a local file starting from the 100th byte
$sftp->put('filename.remote', 'filename.local', SFTP::SOURCE_LOCAL_FILE, 100);

// Upload a local file starting from the 100th byte and start writing at the 50th byte of the remote file
$sftp->put('filename.remote', 'filename.local', SFTP::SOURCE_LOCAL_FILE, 50, 100);

Preserving the Date

If you want the uploaded or downloaded file to have the same last modified/accessed time as the original file, you can enable date preservation:
$sftp->enableDatePreservation();
This can be disabled with the disableDatePreservation function. By default, date preservation is disabled.