PHP ftp_put 失败,并出现“警告:ftp_put (): PORT 命令成功"

2022-01-09 00:00:00 ftp php

文件是在 FTP 服务器上创建的,但它总是 0 字节大.请给我一个解决方案,以便文件上传成功.

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.

我不断收到此警告:

警告:ftp_put(): PORT 命令成功在 C:xampphtdocsmailing eskirim-file-simpan2.php 第 30 行
FTP 上传失败!

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

我的脚本是:

<?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:xampphtdocspersuratanfile2detectip.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); 

?>

推荐答案

PHP 默认为主动 FTP 模式.由于无处不在的防火墙/NAT/代理,现在主动模式几乎无法正常工作.

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.

为此调用 ftp_pasv 之后 ftp_login:

ftp_pasv($nyambungkeftp, true);

请参阅关于 FTP 连接模式的我的文章,以了解,为什么您通常需要使用被动模式.

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

相关文章