Here is an example of how to send a file with SFTP:
<?php
class SFTPConnection
{
private $connection;
private $sftp;
public function __construct($host, $port=22)
{
$this->connection = @ssh2_connect($host, $port);
if (! $this->connection)
throw new Exception('Could not connect to $host on port $port.');
}
public function login($username, $password)
{
if (! @ssh2_auth_password($this->connection, $username, $password))
throw new Exception('Could not authenticate with username $username ' .
'and password $password.');
$this->sftp = @ssh2_sftp($this->connection);
if (! $this->sftp)
throw new Exception('Could not initialize SFTP subsystem.');
}
public function uploadFile($local_file, $remote_file)
{
$sftp = $this->sftp;
$stream = @fopen('ssh2.sftp://$sftp$remote_file', 'w');
if (! $stream)
throw new Exception('Could not open file: $remote_file');
$data_to_send = @file_get_contents($local_file);
if ($data_to_send false)
throw new Exception('Could not open local file: $local_file.');
if (@fwrite($stream, $data_to_send) false)
throw new Exception('Could not send data from file: $local_file.');
@fclose($stream);
}
}
try
{
$sftp = new SFTPConnection('localhost', 22);
$sftp->login('username', 'password');
$sftp->uploadFile('/tmp/to_be_sent', '/tmp/to_be_received');
}
catch (Exception $e)
{
echo $e->getMessage() . 'n';
}
?>
The sftp-server is a standalone binary. The internal-sftp is just a configuration keyword that tells sshd to use the SFTP server code built-into the sshd, instead of running another process (what would typically be the sftp-server). The internal-sftp was added much later (OpenSSH 4.9p1 in 2008?) than the standalone sftp-server binary. Using Command-Line SFTP SFTP is Secure File Transfer Protocol. It is similar to ssh, but its primary purpose is to enable file transfers between a local machine and a remote machine, whereas the ssh (Secure Shell) protocol is for opening up a general command shell. The SSH File Transfer Protocol is a network protocol that provides file access, file transfer, and file management functionalities over SSH. By using SFTP, you can easily change, browse, and edit files on your Raspberry Pi. SFTP is easier to set up than FTP once Raspberry Pi OS has SSH enabled. For security reasons, since the November 2016.
- Dec 01, 2020 SFTP stands for Secure File Transfer Protocol (also known as SSH File Transfer Protocol). It is a file transfer protocol such as FTP yet operates over a secure network protocol like SSH. We highly recommend using SFTP for file transferring because: It adds a layer of security.
- Jun 05, 2020 The sftp and scp clients on the IBM i require Public-key authentication to gain access to ssh servers. Password authentication is not allowed with these two clients. The IBM i ssh client can use either Public-key authentication or Password authentication to gain access to ssh servers.
Many Unix environments have the command-line SSH and SFTP client software tools installed. This page is intended as a guide to just the basics of using these command-line tools. They have many more features than what is described here -- but these instructions should get you started.
Where would I use these?
CS account
- If you have a CS account, you have access to at least three machines you can log into:
- shell.cs.fsu.edu
- program.cs.fsu.edu
- linprog.cs.fsu.edu
- You might also have an account on websrv2.cs.fsu.edu. This would not mount your normal CS account directory, so you might need to file transfer between this machine and others
Mac OS X home computer / laptop
- If you have a Macintosh laptop or home computer running OS X, you cannot use the Windows SSH client unless you're running Windows emulation or have booted into Windows
- In this case, you can use the terminal application (which takes you into a Unix prompt on your Mac) and run the command line SSH and SFTP programs from there
- This is useful for logging into your CS account remotely, as well as doing file transfers between your Mac and your CS account
- To open the terminal application on a Mac:
- Go to the spotlight icon in the upper right of your desktop
- In the search box, type 'terminal'
- A menu choice for the terminal application should appear. Select this
Using command-line SSH
Login format:In this format,
If you leave out the username, the command will assume that you are logging into another machine with the same username as the machine you're currently on.
Examples
- Suppose I'm on a Mac, logged in with user account 'john'.
This command would log me into shell.cs.fsu.edu as username 'smith':This command would log me into shell.cs.fsu.edu, attempting username 'john' (probably will fail, since john is probably not your CS account username!): - You can also log into the machines program or linprog:
Ssh Sftp Command
Using Command-Line SFTP
SFTP is Secure File Transfer Protocol. It is similar to ssh, but its primary purpose is to enable file transfers between a local machine and a remote machine, whereas the ssh (Secure Shell) protocol is for opening up a general command shell on a remote machine where you have an account.The login format for command line sftp is exactly the same as with the ssh command, but with the 'sftp' command:Using the same basis of the prior example, this command would log me into shell.cs.fsu.edu with the sftp (file transfer) program as username 'smith':
Local vs. Remote machines
Before you transfer files, make sure you know the difference between the local and remote machines:- The local machine is the one you started from -- i.e. the machine you were on when you typed the 'sftp' command. This might be your actual physical computer, like a MacBook, if you're using the terminal application. This might also be a machine you are logged into through ssh
- The remote machine is the one you just logged into via the sftp command
Examples
- Suppose I'm on my MacBook, with username john. I open the terminal application and sftp into linprog.cs.fsu.edu with username smith:In this case, the local machine is my MacBook, and the remote machine is linprog
- Suppose I've already logged into user account smith on shell.cs.fsu.edu through an ssh command (or any SSH client). From my CS account, I type this command:Now I have logged into the machine websrv2, with the same user account smith. In this case, the local machine is shell.cs.fsu.edu, and the remote machine is websrv2.cs.fsu.edu.
Basic file transfer commands


- put -- copy a file from the local machine to the remote machine
- get -- copy a file from the remote machine to the local machine
- ls -- get a directory listing on the remote machine
- cd -- change your current working directory on the remote machine
- lls -- get a directory listing on the local machine
- lcd -- change your current working directory on the local machine

If you want to get a directory listing or change directories on the local machine, use the lls (local list files) and lcd (local change directory) commands instead.
I'm not going to describe these commands (listing and change directories) any further, as readers of this file should already be familiar with these unix commands
Ssh Sftp Commands
put and get commands
Once you have used the listing and navigation commands to put yourself in the correct directories, use get to retrieve files from the remote machine to the local account, and use put to send files from your local account to the remote account. The formats of these commands are simple, and you may use unix wildcards.Format:Note that the second parameter of each command is optional -- and it will serve to specify a different name or destination directory for the transferred file. Most of the time, it's easiest to simply use put and get with a single parameter, and the destination will default to the current remote or local directory, respectively.
Examples
- This command will copy the file mycode.cpp from the remote machine to the local one:
- This command will copy the file mypaper.doc from the local machine to the remote one:
- This command will copy the file assign1.cpp from the remote machine, and save it as smith1.cpp on the local machine:
- This command will copy all files ending in .cpp in the current directory on the local machine to the remote machine:
Other SFTP commands
To see a full list of SFTP commands and their formats, you can type help when you are logged in via sftp, and it will give you a list of available commands.To exit the sftp login, you can use either the quit or the exit command.
