Sftp from Within PHP

SFTP from within PHP

Yes, you can do that with cURL. To switch from FTP to SFTP all you have to do is to change protocol option form CURLPROTO_FTP to CURLPROTO_SFTP.

cURL supports following protocols: HTTP, HTTPS , FTP, FTPS, SCP, SFTP, TELNET, LDAP, LDAPS, DICT, FILE, TFTP.

BTW. SFTP is not to be confused with FTPS. SFTP is SSH File Transfer Protocol, while FTPS is FTP over SSL.

connect to a server via sftp php

You might have an easier time using phpseclib, a pure PHP SFTP client. Here's an example:

<?php
include('Net/SFTP.php');

$sftp = new Net_SFTP('www.domain.tld');
if (!$sftp->login('username', 'password')) {
exit('Login Failed');
}

echo $sftp->pwd() . "\r\n";
$sftp->put('filename.ext', 'hello, world!');
print_r($sftp->nlist());
?>

The problem with libssh2, as everyone's recommending, is that it's not very widely deployed, it's API isn't exactly the most reliable, it's unreliable, poorly supported, etc.

How I can move file on a SFTP server in PHP

Use ssh2_sftp_rename:

ssh2_sftp_rename($sftp, $path_from, $path_to);

Assuming both variables contain full paths to files, e.g. like:

$path_from = "/source/directory/myfile.txt";
$path_to = "/destination/directory/myfile.txt";

How to SFTP upload files from PHP

http://phpseclib.sourceforge.net/documentation/intro.html#intro_usage_correct

Per that, phpseclib's root directory needs to be in your include_path. From that page:

<?php
set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');

include('Net/SFTP.php');
?>

You should really familiarize yourself with this kind of include technique - it's pretty standard for PEAR libraries and Zend ones.

Linux PHP Site Converting from FTP to SFTP

The origin of the ssh2 package is PECL but it's repackaged by most distribution of PHP e.g. Ubuntu, Arch Linux, MS-Windows.There's only some very, VERY edge cases where it makes sense to install directly from PECL.

You're not going to make any headway until you have a running SFTP server (any mainstream Linux distro will suffice, SFTP is installed automatically with ssh).

It might share three letters with the protocol you are currently using but SFTP is very different from FTP. However it should be possible (for example) to create functions with one-to-one mappings to what you are currently using, however as the FTP functions are part of the core PHP distribution, you'll need to use different names.



Related Topics



Leave a reply



Submit