Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check is SFTP directory exists before trying to create it using phpseclib

I have the following function in my model:

public function createfolder($location, $name){
    define('NET_SFTP_LOGGING', NET_SFTP_LOG_COMPLEX);
    $sftp = new Net_SFTP('xx.xxx.xx.xx');
    if (!$sftp->login('admin', '********')) {
        exit('Login Failed');
    }
    //moves to a location (Job folder for example)
    $sftp->chdir($location);
    //makes the folder
    $sftp->mkdir($name);
}

This will work but I would like to add some sort of error prevention validation, how can I check if a folder exists using SFTP?


Think I came up with a solution:

chdir() changes directories, mkdir() creates directories, and rmdir() removes directories. In the event of failure, they all return false. chdir(), mkdir(), and rmdir() return true on successful completion of the operation.

So I can use an if statement to check if chdir() === true or false to see if the directory is there.

like image 361
Someone Avatar asked Oct 24 '25 19:10

Someone


1 Answers

You can use $sftp->is_dir($location) to check if it's a directory.

like image 96
ownking Avatar answered Oct 26 '25 10:10

ownking