0

I'm using a plugin for uploading files to a server in WordPress but I cannot get it to work using SFTP on port 22. My server is enabled to use SFTP and I can connect using SFTP using a FTP program, but when I input port 22 into the plugin code it won't connect and says that it cannot create the directory. If I change it to port 21 or remove the port number it works perfectly.

The line I'm putting the port number into is

$conn_id = ftp_connect($ftp_server, 22);

Does anyone know why it won't work on port 22?

The bulk of the plugin code is below with the port number removed in case I am putting it in the wrong place.

$webelephants_company_dir = webelephants_make_folder_name($webelephants_company_name);

$webelephants_ftp_setting = get_option( 'webelephants_ftp_setting' );
$ftp_server = $webelephants_ftp_setting['ftp_server'];
$ftp_user_name = $webelephants_ftp_setting['ftp_user_name'];
$ftp_user_pass = $webelephants_ftp_setting['ftp_user_pass'];

$check_dir = false;
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
$current_dir = ftp_pwd($conn_id);

if ($current_dir !== false && @ftp_chdir($conn_id, $webelephants_company_dir))
{
    $check_dir = true;
    ftp_chdir($conn_id, $current_dir);   

}else {
    ftp_mkdir($conn_id, $webelephants_company_dir);
    ftp_chmod($conn_id, 0777, $webelephants_company_dir);
}

if($check_dir == false && @ftp_chdir($conn_id, $webelephants_company_dir)){
    $check_dir = true;
}   

ftp_close($conn_id);

if($check_dir == false) {
    $webelephants_form_error = 'Could not create directory.';
}

if($webelephants_form_error == '')
{
    /*setcookie('webelephants_company_dir', $webelephants_company_dir, 0, COOKIEPATH, COOKIE_DOMAIN,false,true);
    if ( SITECOOKIEPATH != COOKIEPATH ){
        setcookie('webelephants_company_dir', $webelephants_company_dir, 0, SITECOOKIEPATH, COOKIE_DOMAIN,false,true);
    }*/

    //$_SESSION['webelephants_company_dir'] = $webelephants_company_dir;

    if($file_count > 0) {

        $conn_id = ftp_connect($ftp_server);
        $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

        if(@ftp_chdir($conn_id, $webelephants_company_dir))

        {
            ftp_pasv($conn_id, true);

            for ($i = 0; $i <= $file_count; $i++) {

                if(isset($_FILES['webelephants_file']['name'][$i])) {
                    $remote_file = $_FILES['webelephants_file']['name'][$i];
                    $local_file = $_FILES['webelephants_file']['tmp_name'][$i];
                    $ret = ftp_nb_put($conn_id, $remote_file, $local_file, FTP_BINARY);

                    while ($ret == FTP_MOREDATA) {
                       set_time_limit(0);

                       //flush();

                       //sleep(1);

                       //usleep(20000);

                       $ret = ftp_nb_continue($conn_id);
                    }

                    //fclose($fo);

                    if ($ret != FTP_FINISHED) {

                       $webelephants_form_error .= $_FILES['webelephants_file']['name'][$i].": There was an error uploading the file...<br>";
                    }
                }
            }

0 Answers0