Quick Storage API in PHP with the SFTP protocol
Published {$created} by Carsten Blum
You don’t always need a heavyweight SDK to get reliable cloud storage. With SFTP, PHP can act as a lightweight Storage API that’s both secure and easy to automate.In this tutorial, you’ll generate a simple text file and upload it to ftpGrid using the phpseclib library.
Before starting this tutorial, you need an account with ftpGrid, which is a simple process outlined in our getting started with FTP/SFTP guide. If your are in doubt if managed FTP/SFTP hosting is for you, you can read our guide to ftpGrid's managed hosting. If you need help creating your SSH keys, follow our SSH key creation tutorial.
Basic knowledge with this programming language is assumed in this tutorial.
Requirements
PHP 8.0+
Composer
An ftpGrid account with SFTP access
Library:
phpseclib/phpseclib
(v3)
Install the dependency:
composer require phpseclib/phpseclib:^3
Example: Generate and Upload a File (Password Auth)
Here’s a complete example that creates hello.txt
, ensures the /uploads
directory exists on the server, and uploads the file via SFTP.
Create upload.php
with:
<?php
declare(strict_types=1);
use phpseclib3\Net\SFTP;
require __DIR__ . '/vendor/autoload.php';
// Step 1: Generate a local file
$localFile = __DIR__ . '/hello.txt';
file_put_contents($localFile, "Hello from PHP via ftpGrid SFTP!\n");
// Step 2: Connect via SFTP (password auth)
$host = 'edge1.ftpgrid.com';
$port = 22;
$user = 'your_username';
$pass = 'your_password';
$sftp = new SFTP($host, $port);
if (!$sftp->login($user, $pass)) {
fwrite(STDERR, "Login failed\n");
exit(1);
}
// Step 3: Ensure remote directory exists
$remoteDir = '/uploads';
if (!$sftp->is_dir($remoteDir)) {
$sftp->mkdir($remoteDir, -1, true); // recursive mkdir
}
// Step 4: Upload the file
$remoteFile = $remoteDir . '/hello.txt';
if (!$sftp->put($remoteFile, file_get_contents($localFile))) {
fwrite(STDERR, "Upload failed\n");
exit(1);
}
echo "Uploaded to {$remoteFile}\n";
?>
Run the script:
php upload.php
Now log in to ftpGrid or connect with another SFTP client, and you should see hello.txt
in /uploads
.
Using SSH Keys (Recommended)
Passwords work, but SSH keys are more secure. phpseclib makes it simple to use key-based authentication:
<?php
declare(strict_types=1);
use phpseclib3\Net\SFTP;
use phpseclib3\Crypt\PublicKeyLoader;
require __DIR__ . '/vendor/autoload.php';
// Step 1: Generate a local file
$localFile = __DIR__ . '/hello.txt';
file_put_contents($localFile, "Hello from PHP with SFTP key auth!\n");
// Step 2: Load private key and connect
$host = 'edge1.ftpgrid.com';
$port = 22;
$user = 'your_username';
$keyPath = getenv('HOME') . '/.ssh/id_ed25519';
$privateKey = PublicKeyLoader::loadPrivateKey(file_get_contents($keyPath));
$sftp = new SFTP($host, $port);
if (!$sftp->login($user, $privateKey)) {
fwrite(STDERR, "Key login failed\n");
exit(1);
}
// Step 3: Ensure remote directory exists
$remoteDir = '/uploads';
if (!$sftp->is_dir($remoteDir)) {
$sftp->mkdir($remoteDir, -1, true);
}
// Step 4: Upload the file
$remoteFile = $remoteDir . '/hello.txt';
$sftp->put($remoteFile, file_get_contents($localFile));
echo "Uploaded with key authentication to {$remoteFile}\n";
?>
Conclusion
With just a few lines of PHP and phpseclib, you now have a functioning Storage API powered by SFTP.No complex SDKs, just a secure, standards-based way to transfer files to ftpGrid.
This pattern can easily be extended for:
Automated backups
Data ingestion pipelines
Secure file sharing
Create your free ftpGrid account →
Next in the Quick Storage API series: We’ll explore the same example in Java.