简体   繁体   English

使用PHP要求通过TLS的显式FTP

[英]Require explicit FTP over TLS with PHP

I'm trying to upload file to ftp with php. 我正在尝试使用php将文件上传到ftp。 Only requirement from server's admin were Encryption should be Require explicit FTP over TLS . 服务器管理员的唯一要求是加密应为Require explicit FTP over TLS And that's it but I'm getting error Warning: ftp_fput() [function.ftp-fput]: Can't open data connection. 就是这样,但是我收到错误Warning: ftp_fput() [function.ftp-fput]: Can't open data connection. . I manage to connect(in passive mode), but it fails to upload with any function( ftp_put , ftp_nb_put , ftp_fput ). 我设法连接(处于被动模式),但无法使用任何功能( ftp_putftp_nb_putftp_fput )上传。 Error is the same all the times. 错误一直都是一样的。 I'm trying following code: 我正在尝试以下代码:

$host = 'ftps.someserver.net';
$user = '****';
$pass = '****';
$filename = "somefile.txt"


$dest_file = $filename;
$source_file = dirname(__FILE__) . '/' . $filename;

$fp = fopen($source_file, 'r');

$ftp = ftp_ssl_connect($host, 21, 180);


if (ftp_login($ftp,$user,$pass)) {

    var_dump(ftp_pasv($ftp, true)); // gives me true
    // # first way
    // ftp_put($ftp, $dest_file, $source_file, FTP_BINARY);

    // # second
    // $ret = ftp_nb_put($ftp, $dest_file, $source_file, FTP_BINARY, FTP_AUTORESUME);

    // while (FTP_MOREDATA == $ret)
    // {
    //   $ret = ftp_nb_continue($ftp);
    // }

    // # third
    if (ftp_fput($ftp, $dest_file, $fp, FTP_ASCII)) { // I was trying FTP_BINARY and FTP_ASCII.
        echo "Successfully uploaded\n";
    } else {
        echo "There was a problem while uploading\n";
    }       
}
ftp_close($ftp);
fclose($fp);

It works fine with my own ftp, that does not asks for explicit TLS. 它与我自己的ftp可以正常工作,该ftp不需要显式TLS。 Any ideas would be appreciated. 任何想法,将不胜感激。

Try using ftp_pasv($ftp, TRUE); 尝试使用ftp_pasv($ftp, TRUE); . I found this in a five year old comment on php.net, and it worked for me. 我在php.net上有五年历史的评论中发现了这一点,并且对我有用。

This one got me too. 这也是我的意思。 I discovered it was a bug with the version of PHP I was using (5.4.28). 我发现这是我使用的PHP版本(5.4.28)的错误。 I tried the same script with PHP version 5.6.31 and everything worked. 我在PHP 5.6.31版中尝试了相同的脚本,并且一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM