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>