Connect to sFTP Server in PHP

Connect to sFTP Server in PHP

These days, seamless data transfer and secure file management are integral components of web development and data-driven applications. The need to establish a secure connection to an SFTP (Secure File Transfer Protocol) server through PHP arises in scenarios where sensitive information, such as user data or confidential files, must be transmitted and managed securely. SFTP provides a secure and encrypted channel for transferring files over a network by ensuring data integrity and confidentiality.

To use SFTP within your PHP application, a series of fundamental steps need to be executed. First and foremost, you must establish a connection to the SFTP server by utilizing the ssh2 extension, which facilitates secure communication. Subsequently, authentication is crucial to ensure the identity of the connecting entity. This is achieved by providing valid credentials, including a username and password, to the SFTP server. Once authenticated, your PHP script gains access to the server and enables file-related operations such as listing directory contents, downloading files to the local environment, and uploading files to the remote server.

The following PHP script demonstrates these steps for interacting securely with SFTP servers and facilitating robust file management in your web applications.

To connect to an SFTP server, list files, download, and upload files using PHP, you can use the ssh2 extension. Make sure the extension is installed and enabled on your server.

<?php

$server = 'your.sftp.server';
$port = 22;
$username = 'your_username';
$password = 'your_password';

// Connect to the SFTP server
$conn = ssh2_connect($server, $port);
if (!$conn) {
    die('Connection failed');
}

// Authenticate with username and password
if (!ssh2_auth_password($conn, $username, $password)) {
    die('Authentication failed');
}

// List files on the server
$remoteDir = '/path/to/remote/directory';
$files = ssh2_sftp_nlist($conn, $remoteDir);

// Print the list of files
echo "List of files on the server:\n";
print_r($files);

// Download a file
$remoteFile = '/path/to/remote/file.txt';
$localFile = '/path/to/local/file.txt';

if (ssh2_scp_recv($conn, $remoteFile, $localFile)) {
    echo "File downloaded successfully.\n";
} else {
    echo "Failed to download the file.\n";
}

// Upload a file
$localFile = '/path/to/local/upload.txt';
$remoteFile = '/path/to/remote/upload.txt';

if (ssh2_scp_send($conn, $localFile, $remoteFile, 0644)) {
    echo "File uploaded successfully.\n";
} else {
    echo "Failed to upload the file.\n";
}

// Close the SSH connection
ssh2_disconnect($conn);

?>

Categories: PHP Source Code
M. Saqib: Saqib is Master-level Senior Software Engineer with over 14 years of experience in designing and developing large-scale software and web applications. He has more than eight years experience of leading software development teams. Saqib provides consultancy to develop software systems and web services for Fortune 500 companies. He has hands-on experience in C/C++ Java, JavaScript, PHP and .NET Technologies. Saqib owns and write contents on mycplus.com since 2004.
Related Post