Home >Backend Development >PHP Tutorial >How Can I Securely Transfer Files Using SFTP in PHP?
To securely transfer files over the network, SFTP (SSH File Transfer Protocol) emerges as a reliable option. With PHP, you can seamlessly incorporate SFTP capabilities into your web applications.
PHP offers built-in SSH2 stream wrappers that enable you to establish SFTP connections. These wrappers work with any function that supports stream wrappers, allowing you to use a simple syntax like:
$contents = file_get_contents('ssh2.sftp://user:[email protected]:22/path/to/filename');
If you prefer a more explicit approach, you can utilize the PHP SSH2 extension. The following code snippet demonstrates how:
$connection = ssh2_connect('shell.example.com', 22); ssh2_auth_password($connection, 'username', 'password'); $sftp = ssh2_sftp($connection); $stream = fopen("ssh2.sftp://$sftp/path/to/file", 'r');
For more detailed information, refer to the PHP manual on SSH2 stream wrappers: http://php.net/manual/en/wrappers.ssh2.php
Also, numerous discussions on this topic can be found on Stack Overflow: https://stackoverflow.com/search?q=sftp php
The above is the detailed content of How Can I Securely Transfer Files Using SFTP in PHP?. For more information, please follow other related articles on the PHP Chinese website!