如何在 php 中使用 fopen 打开 ftps 路径;未收到来自的标识字符串

How to open ftps path using fopen in php; Did not receive identification string from

我正在尝试将字符串数据推送到远程 ftp 路径。我以前在其他设置中这样做过,但在这种情况下,服务器似乎无法识别标识字符串。这可能只是 ftp 路径字符串上的一个小语法错误,但我看不到它。

我知道我正在访问远程服务器,因为我可以检查日志文件并看到消息

Did not receive identification string from xxx.xxx.xxx.xxx

对于我所做的每一次尝试。

挂起一段时间后我的脚本只是 returns 错误

Warning: fopen() failed to open stream.

我的文件路径格式如下:

$ftp_path = "ftps://user:password@remoteaddress:12345/path/to/file/filename.txt";//port 12345 is deliberate because I use port forwarding on the server to reduce failed hack attempts

处理连接的脚本部分如下:

$ftp_path = "ftps://user:password@remoteaddress:12345/path/to/file/filename.txt";
// Allow/disallow overwriting of existing files on the remote FTP server
$stream_options = array('ftps' => array('overwrite' => false));
// Creates a stream context resource with the defined options
$stream_context = stream_context_create($stream_options);
// Opens the file for writing and truncates it to zero length 
if ($fh = fopen($ftp_path, 'w', 0, $stream_context)){
    // Writes contents to the file
    if(fputs($fh, $content)){
        echo "records pushed successfully.";
    } else {
        echo "The stream was opened, but the records failed to push.";
    }
    // Closes the file handle
    fclose($fh);
} else {
    die('Could not open remote ftp.');
}

此错误可能表示您正试图通过 FTP 包装程序访问 SFTP/SSH 服务器。 FTP/FTPS 和 SSH/SFTP 是不兼容的协议,因此不要将 FTPS 与 SFTP.

混淆