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
Post and Get at the Same Time in PHP
Print Time in 15-Minute Increments Between Two Times in the Same Day
How to Sum N Number of Time (Hh:Mm Format)
PHP Regex to Check Date Is in Yyyy-Mm-Dd Format
How to Rename Sub-Array Keys in PHP
How to Remove Blank Lines from Text in PHP
How to Check If an Integer Is Within a Range
Sharing Session Variables Between Multiple Subdomains
How to Read a .Tar.Gz File with PHP
PHP Memcached Fatal Error: Class 'Memcache' Not Found
Pre-Declare All Private/Local Variables
PHP Exec $Path Variable Missing Elements
Access Denied for User 'Homestead'@'Localhost' (Using Password: Yes)
How to Create a Database Using Pdo in PHP
How to Generate a Random Key Within PHP
Php. How to Use Array_Column with an Array of Objects
How to Remember Input Data in the Forms Even After Refresh Page