PHP ftp_put fails with “Warning: ftp_put (): PORT

2019-02-14 19:27发布

问题:

File is created on the FTP server, but its always 0 bytes large. Please give me a solution so that the file upload will working success.

I keep getting this warning:

Warning: ftp_put (): PORT command successful in C: \ xampp \ htdocs \ mailing \ teskirim-file-simpan2.php on line 30
FTP upload has failed!

My script is:

<?php
$ftp_server = "********";
$ftp_serverpath = "ftp.".$ftp_server;
$ftp_user_name = "********";
$ftp_user_pass = "***********";
$email_dir = "*******@*********";

$nyambungkeftp = ftp_connect($ftp_server);
if (false === $nyambungkeftp) {
    throw new Exception('Unable to connect');
}

$loggedInnyambungkeftp = ftp_login($nyambungkeftp,  $ftp_user_name,  $ftp_user_pass);
if (true === $loggedInnyambungkeftp) {
    echo 'Success!';
} else {
    throw new Exception('Unable to log in');
}

if ((!$nyambungkeftp) || (!$loggedInnyambungkeftp)) { 
        echo "FTP connection has failed!";
        echo "Attempted to connect to $ftp_server for user $ftp_user_name"; 
        exit; 
    } else {
        echo "Connected to $ftp_server, for user $ftp_user_name";
    }

// upload the file
$dest = 'detectip.txt';
$source = 'C:\xampp\htdocs\persuratan\file2\detectip.txt';

echo $dest;
echo $source;
$upload = ftp_put($nyambungkeftp, $dest, $source, FTP_ASCII); 


// check upload status
if (!$upload) { 
        echo "FTP upload has failed!";
    } else {
        echo "Uploaded $source_file to $ftp_server as $destination_file";
    }

// close the FTP stream 
ftp_close($nyambungkeftp); 

?>

回答1:

PHP defaults to the active FTP mode. The active mode hardly ever works these days due to ubiquitous firewalls/NATs/proxies.

You almost always need to use the passive mode.

For that call the ftp_pasv after the ftp_login:

ftp_pasv($nyambungkeftp, true);

See my article on FTP connection modes, to understand, why you typically need to use the passive mode.



回答2:

Try two things:

  1. Try FTP_BINARY instead of FTP_ASCII
  2. Try to use passive mode doc here


标签: php ftp