Skip to content
Advertisement

SFTP Opendir failing when called via Function

I have a bunch of php pages that are running on a schedule that pull data from different SFTP sources, to try and minimise this as a temporary fix I am turning them into functions and having one page that calls each of them

However, when converting these to functions the pages are giving me error 500, through process of elimination I have found that it is when it using the opendir function via sftp

I have called the function page by itself with a reference to call itself as a test and it connects fine, but when called from another page it errors out.

The variables being used to open the directory via sftp are generated on the ‘required’ page so it’s not losing anything via session variables

If I point it to an incorrect directory it is being caught by the error handling in place, but when it ‘successfully’ connects I get the error 500

Page1.php

<?php
require 'Page2.php';
sftpFunc();
?>

Page2.php

<?php
Function sftpFunc()
{

/* variable declarations and value assignments go here*/


if (!$FTP_CONN = ssh2_connect($FTP_HOST, $FTP_PORT))
            die('Unable to connect');
if (!ssh2_auth_password($FTP_CONN, $FTP_USER, $FTP_PASS))
            die('Unable to authenticate.');
if (!$FTP_STRE = ssh2_sftp($FTP_CONN))
            die('Unable to create a stream.');
if (!$FTP_OPEN = opendir("ssh2.sftp://{$FTP_STRE}{$FTP_DIRI}"))
    die('Could not open the directory');
}
?>

Advertisement

Answer

Updated the php version on the site from 7.2.34 to 7.3.11 and it seems to have done the trick, hadn’t seen any version specific issues with opendir() before and it seemed to only happen when using it through a function on a different page.

Not sure on the why, but at least this worked.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement