dongpo2458 2018-06-11 18:27
浏览 63
已采纳

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

I'm trying to push string data to a remote ftp path. I've done this before in other settings, but in this case the server doesn't seem to recognize the identification string. This is probably just a small syntax error on the ftp path string, but I can't see it.

I know that I'm reaching the remote server because I can check the log file and see the message

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

for each attempt that I make.

After hanging for a while my script simply returns the error

Warning: fopen() failed to open stream.

My file path is formatted thusly:

$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

The part of the script that deals with the connection is as follows:

$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.');
}
  • 写回答

1条回答 默认 最新

  • dongyongju9560 2018-06-13 20:55
    关注

    This error may indicate you are attempting to access an SFTP/SSH server through the FTP wrapper. FTP/FTPS and SSH/SFTP are not compatible protocols so don't confuse FTPS with SFTP.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?