I took over a php website running on Linux. We need to convert from using ftp to sftp. The code prompts the user for the local file and then uses ftp to put the file on the local server. Currently we are using the following:
$ftp_user = FTP_USER;
$ftp_pass = FTP_PASS;
$ftp_server = FTP_SERVER;
$ftp_destination_path = FTP_DESTINATION_PATH;
$url_path = "uploads/";
if (!($conn_id = @ftp_connect($ftp_server)))
return('error connecting');
if (!($login_result = @ftp_login($conn_id, $ftp_user, $ftp_pass)))
return('error logging in');
$filename_fixed = str_replace(" ", "_", $file['name']);
$filename_fixed = str_replace("/", "-", $filename_fixed);
$destination_file = $ftp_destination_path . $filename_fixed;
if (!($upload = @ftp_put($conn_id, $destination_file, $file['tmp_name'], FTP_BINARY)))
die("Cannot upload to location $destination_file");
ftp_close($conn_id);
Is there anything native in php for sftp? Seems I have to install third party package like PECL ssh2? I need help as I can't find a good example for how to convert what I have to sftp?
CodePudding user response:
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.