首页 > 解决方案 > php:@fopen 在 localhost 中有效,但在实时服务器中无效

问题描述

我正在尝试获取这样的远程文件:

$host = 'sftp.hostname.com';
$username = 'user';
$password = '*****';
$port = '22';
$remote_file = 'TOB-20180919.text';
$connection = @ssh2_connect($host, $port);
if ($connection != false) {
    $login = @ssh2_auth_password($connection, $username, $password);
    if ((bool) $login) {
        $sftp = @ssh2_sftp($connection);
        if ($sftp != false) {
            $stream = @fopen("ssh2.sftp://$sftp" . "/" . "$remote_file", 'w');
            var_dump($stream);exit;
        }
    }
}

$streamfalse在我的实时服务器和本地主机中打印true。我现在该怎么办?

标签: phpsftpfopen

解决方案


我遇到了同样的问题,所以我使用以下代码解决了这类问题。请使用它并检查。

$stream = @fopen("ssh2.sftp://".(int)$sftp."/".$remote_file, 'w');

代替

$stream = @fopen("ssh2.sftp://$sftp" . "/" . "$remote_file", 'w');

推荐阅读