0
votes

I got a weird problem using ftp_put.

My code works perfectly when connecting to my old linux server (ftp_connect), but fails when trying to upload files to the new windows server (client choice) - we're running Filezilla Server on the windows server.

  • Can connect to the windows server works (ftp_connect).
  • Creating folders works (ftp_mkdir).
  • Uploading files does not work - no idea why (ftp_put).
  • I can upload files using FileZilla.
  • User has full permissions on the Filezilla Server.
  • Connecting to "C:/FTP" when connecting to the windows server.

I'm completely lost, got no idea what is causing these errors. Maybe one of you might be able to point me in the right direction.

Does my code need to change path, when changing from a linux to windows server? The folder structure is 100% identical (file has to be saved within a folder named "SE").

This is my code:

# Conf ID
$conf_id = '7AEHQ6GS'; // for testing

# Files
$file1 = "xml/$conf_id/cylinder-tube.xml";

# FTP info
$ftp_host = '';
$ftp_port = '';
$ftp_user = '';
$ftp_pass = '';


# Connect to FTP server
$conn_id = ftp_connect($ftp_host, $ftp_port, 10);

# Connect to FTP Server
if($conn_id) {

    if(@ftp_login($conn_id, $ftp_user, $ftp_pass)) {

        if(file_exists($file1)) {

            # Change directory
            ftp_chdir($conn_id, 'SE');

            # Create folder
            ftp_mkdir($conn_id, $conf_id);

            # Change directory
            ftp_chdir($conn_id, $conf_id);

            # Upload files
            if(
                ftp_put($conn_id, basename($file1), $file1, FTP_BINARY)
            ) {

                # Delete local files
                @unlink($file1);

                # Delete local folder
                rmdir("xml/$conf_id");

            }else{
                $st['status'] = 'FTP003';
                die(json_encode($st));
            }
        }else{      
            $st['status'] = 'FEJL011';
            die(json_encode($st));
        }
    } else {
        $st['status'] = 'FTP002';
        die(json_encode($st));
    }
}else{
    $st['status'] = 'FTP001';
    die(json_encode($st));
}

This is where my code is failing:

ftp_put($conn_id, basename($file1), $file1, FTP_BINARY)

Thanks,

Kenneth

1
ftp_put($conn_id, cylinder-tube.xml, xml/7AEHQ6GS/cylinder-tube.xml, FTP_BINARY) - Kenneth Poulsen
"xml" folder is on the local-php server. "SE" folder is on the FTP servers :) - Kenneth Poulsen
Ahh!! Sorry my bad. Try with FTP_ASCII. Also, see this stackoverflow.com/questions/40720260/php-ftp-put-fails - nice_dev
It was passive mode! Thanks for the link (already tried FTP_ASCII earlier). Using this worked: ftp_pasv($conn_id, true) or die("Unable switch to passive mode"); - Kenneth Poulsen
Thanks for your help. Can you post an answer with the link and ftp_pasv, so I can accept your solution? :) - Kenneth Poulsen

1 Answers

2
votes

If ftp_put() is failing, it is quite possible that it is due to a firewall protection. So, this happened because the connection is in ACTIVE mode.

Switch to PASSIVE mode with the help of ftp_pasv() to pass through the firewall security.

More details on active and passive FTP connections can be found here