PHP:读取远程文件(理想情况下使用fopen)

时间:2018-07-29 18:11:59

标签: php fopen phpseclib

我想使用PHP读取远程文本文件(最好使用fopen)。当我在本地文件上使用此功能时,脚本可以使用fopen进行工作。

我尝试过:

$file = fopen ("http://abc.abc.abc", "r");
if (!$file) {
    echo "<p>Unable to open remote file.\n";
    exit;
}

我得到:

  

警告:fopen(http://abc.abc.abc):无法打开流:由于目标计算机主动拒绝连接,因此无法建立连接。第2行的C:\ xampp \ htdocs \ NMR \ nmrTest5.php中无法打开远程文件。

我已经读到phpseclib可能是一个不错的选择,并且因为我可以使用WinSCP(SFTP)或使用Puttyfor来访问我的文件,所以我尝试了此操作(将所有文件从phpseclib复制到我的目录之后)希望我可以在本地复制文件,然后用fopen读取它(这不是满足要求的最好方法,但我可以接受):

include('Net/SFTP.php');

$sftp = new Net_SFTP('abc.abc.abc');
if (!$sftp->login('username', 'pass')) {
    exit('Login Failed');
} 

然后我得到了

  

注意:在1561行的C:\ xampp \ htdocs \ NMR \ Net \ SSH2.php中找不到与客户端加密算法兼容的服务器   登录失败

有趣的是,如果我连接到服务器(使用WinSCP),则会收到不同的消息:

  

注意:在行3362上从C:\ xampp \ htdocs \ NMR \ Net \ SSH2.php中的套接字读取错误

     

注意:连接已在第1471行的C:\ xampp \ htdocs \ NMR \ Net \ SSH2.php中被服务器关闭   登录失败

关于如何使它工作的任何想法?理想情况下,我会使用fopen,但我愿意接受其他解决方案。

3 个答案:

答案 0 :(得分:1)

我曾经遇到过与fopen类似的问题。 卷曲对于这些目的很有用。

请检查以下基本示例功能(如果url为https,请取消注释CURLOPT_SSL_VERIFYPEER = FALSE行)。

$url = '***THE URL***';
$result = get_web_page_by_curl($url);

if ($result['errno'] != 0) echo 'error: bad url, timeout, redirect loop ...';
if ($result['http_code'] != 200) echo 'error: no page, no permissions, no service ...';
else {
    $page = $result['content'];
    echo $page;
}

function get_web_page_by_curl($url) {
    $agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";

    $options = array(
        CURLOPT_RETURNTRANSFER => true,     // return web page
        CURLOPT_HEADER         => false,    // don't return headers
        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
        CURLOPT_ENCODING       => "",       // handle all encodings
        CURLOPT_USERAGENT      => $agent,   // who am i
        CURLOPT_AUTOREFERER    => true,     // set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
        CURLOPT_TIMEOUT        => 120,      // timeout on response
        CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
        //CURLOPT_SSL_VERIFYPEER => FALSE   // this line makes it work under https 
    );

    $ch = curl_init($url);
    curl_setopt_array($ch, $options);
    $content = curl_exec($ch);
    $err     = curl_errno($ch);
    $errmsg  = curl_error($ch);
    $header  = curl_getinfo($ch);
    curl_close($ch);

    $header['errno']   = $err;
    $header['errmsg']  = $errmsg;
    $header['content'] = $content;
    return $header;
}

答案 1 :(得分:1)

我本人只是一直在解决这个确切的问题,在任何一个地方都找不到任何好的文档来完成此工作。

我刚刚制作了一个使用Monolog的日志记录服务,并且基本上是根据要写入/创建的日志文件制作一个自定义流处理程序。因此,它需要一种资源(例如由fopen创建的资源)才能将日志文件写入SFTP服务器。

我可以像这样使用ssh2库来工作:

$connection = ssh2_connect($this->host, 22);
ssh2_auth_password($connection, $this->user, $this->password);

$sftp = ssh2_sftp($connection);

//some stuff to do with whether the file already exists or not

$fh=fopen("ssh2.sftp://$sftp".ssh2_sftp_realpath($sftp,".")."/$this->logName/$this->fileName", 'a+');

return new StreamHandler($fh);

在将服务集成到另一个项目中之前,一切工作都很好,并且意识到这只在我的开发机器上工作,因为它安装了libssh2as outlined in this question

不幸的是,向生产服务器添加库并不是那么容易。因此,我发现自己正在寻找其他解决方案。

我在其他项目中使用过phpseclib,但仅用于基本的get()put()和一些nlist()通话。

为了使此工作正常,我必须使用Stream对象。记录不充分,但是有a good discussion here

根据那里的信息,以及在SFTP类中进行的一些挖掘,特别是get()函数,这就是我设法使用phpseclib

SFTP\Stream::register();
$sftpFileSystem = new SFTP($this->host);
if (!$sftpFileSystem->login($this->user, $this->password)) {
    throw new Exception("Error logging in to central logging system. Please check the local logs and email for details", 1);
}

$context = [
    'sftp' => [
        'sftp' => $sftpFileSystem
    ],
];

//some stuff to do with whether the file already exists or not
$remote_file = $sftpFileSystem->realpath('test.txt');

$sftpStream = fopen("sftp://.{$remote_file}", 'a+', null, stream_context_create($context));
if (!$sftpStream) {
    exit(1);
}

return new StreamHandler($sftpStream);

请注意对.的调用中sftp://后面的点(fopen())。这浪费了我一个半小时!

答案 2 :(得分:1)

这就是我结束使用@neubert在问题注释中建议的phpseclib解决问题的方法。

我首先在服务器上添加了phpseclib文件夹。然后,我在PHP文件中使用以下代码来访问远程服务器上的文件:

//needed for phpseclib
set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');
include_once('Net/SFTP.php');

//connection to the server
$sftp = new Net_SFTP('abc.abc.abc');
if (!$sftp->login('my_login', 'my_password')) {
  exit('Login Failed');
}

//dump entire file in a string, convert to an array and check number of lines

else {
  $text = $sftp->get('full_path_to_my_file');
}
$myArray = explode("\n", $text);
$nbline = count($myArray);