doukao2180 2017-08-11 04:14
浏览 142

通过SSH2检索远程文件适用于PHP 7,但不适用于PHP5.6

So.. this function works great in PHP7.. but I have to downscale the server to 5.6 because of some other code that doesn't work with PHP7.

Once the server is scaled back to 5.6, this SSH2 function throws the Failed to open directory error. I have checked all the functions that are called and they are all PHP 4.0 and up. Anyone have any ideas?

    function get_the_k12_file($filename)
 {
$host = "thehost.com";
$port = 22;
$username = "username!";
$password = "password!";
$remoteDir = "/the/path/tothefile/";
$localDir = "";
  
if (!function_exists("ssh2_connect"))
    die("Function ssh2_connect does not exist.");
  
if (!$connection = ssh2_connect($host, $port))
    die("Failed to connect.");
  
if (!ssh2_auth_password($connection, $username, $password))
    die("Failed to authenticate.");
  
if (!$sftp_conn = ssh2_sftp($connection))
    die("Failed to create a sftp connection.");
  
if (!$dir = opendir("ssh2.sftp://$sftp_conn$remoteDir"))
    die("Failed to open the directory.");
  
$files = array();
while ( ($file = readdir($dir)) !== false)
{
    if(substr($file, -4)==".zip")
    {
        $files[]=$file;
    }
}
closedir($dir);
  
foreach ($files as $file)
{
 
if($file==$filename)
  {
    echo "Copying file: $file
";
    if (!$remote = fopen("ssh2.sftp://$sftp_conn$remoteDir$file", "r"))
    {
        echo "Failed to open remote file: $file
";
        continue;
    }
  
    if (!$local = fopen($localDir . $file, "w"))
    {
        echo "Failed to create local file: $file
";
        continue;
    }
  
    $read = 0;
    $filesize = filesize("ssh2.sftp://$sftp_conn/$remoteDir$file");


    while ( ($read < $filesize) && ($buffer = fread($remote, $filesize - $read)) )
    {
        $read += strlen($buffer);
        if (fwrite($local, $buffer) === FALSE)
        {
            echo "Failed to write to local file: $file
";
            break;
        }
    }
 
   }
    fclose($local);
    fclose($remote);
}
}

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
    • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
    • ¥15 Centos / PETSc / PETGEM
    • ¥15 centos7.9 IPv6端口telnet和端口监控问题
    • ¥120 计算机网络的新校区组网设计
    • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 海浪数据 南海地区海况数据,波浪数据
    • ¥20 软件测试决策法疑问求解答
    • ¥15 win11 23H2删除推荐的项目,支持注册表等