Search code examples
sftpfile-transfer

Uploading a file via website to SFTP server


I'm building a website. I have the SFTP login credentials for the server.

I'm trying to make it so that a user can select a file on their hard-drive, and upload the file to a remote computer through SFTP.

Is this possible? How would I do this?


Solution

  • I assume you use (or can use) PHP. You didn't specify, what technology you are using.

    Start with reading:

    That combined together gets you a code like:

    include('Net/SFTP.php');
    
    $uploaded_file = $_FILES["attachment"]["tmp_name"];
    
    $sftp = new Net_SFTP("example.com");
    if (!$sftp->login('username', 'password'))
    {
        die("Connection failed");
    }
    
    $sftp->put(
        "/remote/path/".$_FILES["attachment"]["name"],
        file_get_contents($uploaded_file));
    

    This is a very simplified code, lacking lots of validation and error checking.

    The code uses the phpseclib library.