doushi9474
doushi9474
2014-09-22 16:19
浏览 27
已采纳

如何使用php检查ssh服务器中的文件是否为空

I am connecting to a SSH server using PHP and there is file called difference in the server. I want to check whether it is empty or not and echo something depending on the result.

I am aware of the function :

file_get_contents("./difference")

But this will work in in the localhost directory. I want to check in the SSH server. How Should i proceed...?

PS : I use ssh2 to connect to ssh server

图片转代码服务由CSDN问答提供 功能建议

我使用 PHP 连接到 SSH服务器 服务器中有文件名为差异。 我想检查它是否为空,并根据结果回显。

我知道 函数:

  file_get_contents(“./ difference”)
   
 
 

但是这将在< code> localhost 目录。 我想检查SSH服务器。 我该怎么办......?

PS:我使用 ssh2 连接到ssh服务器

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dqp99585
    dqp99585 2014-09-22 17:00
    已采纳

    you can use this ways :

    1) use ssh2.sftp protocol :

    $data  = file_get_contents('ssh2.sftp://user:pass@youraddress.com:22/path/to/one/file');
    

    2) use ssh2

    $ssh2_conn = ssh2_connect('youraddress.com', 22);
    ssh2_auth_password($ssh2_conn, 'username', 'password');
    $sftp_conn = ssh2_sftp($ssh2_conn);
    $file_stream = fopen("ssh2.sftp://$sftp_conn/path/to/one/file", 'r');
    
    点赞 评论
  • duanqi6007
    duanqi6007 2014-09-22 16:33

    You could try:

    if (0 == filesize($path_to_difference)){
    echo "File is empty!";
    } else {
    echo "File has data inside!";
    }
    

    Is that what you're looking for?

    点赞 评论

相关推荐