search
HomeBackend DevelopmentPHP ProblemHow to implement sftp upload in php

php method to implement sftp upload: 1. Create the code "class SFTPConnection private $connection...try{...}catch{...}"; 2. Execute "sftp -oPort=port user @server" statement is enough.

How to implement sftp upload in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How does php implement sftp upload?

php Implement SFTP file upload

php To implement SFTP file upload, you can completely use the method on the php.net official website. The code is as follows:

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";
}

But I encountered a problem during the process. My php version is PHP 5.6.31 (cli) (built: Aug 2 2017 15:05:23). When executing

$stream = @fopen("ssh2.sftp://$sftp$remote_file", 'w');

fopen, execute the file A "Segmentation fault" error will be reported, and then it can be solved in the following way

$stream = @fopen("ssh2.sftp://" . intval($sftp) . $remote_file, 'w');

Among them, when implementing sftp upload, the difference between uploaded files and upload directories is not paid attention to (for example: /upload and /upload /test.txt), causing fopen(): Unable to open ssh2.sftp://5/upload on remote host to be reported every time php is executed. The solution to the problem is Serious, capitalized Serious

The above is done by php. Just log in to the sftp server to check and you will know the result.

sftp command login method:

sftp -oPort=port user@server and then enter the password , after entering, you can go to the relative directory to check whether the file exists.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to implement sftp upload in php. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),