php - 在尝试使用 phpseclib 创建 SFTP 目录之前检查它是否存在

我的模型中有以下函数:

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);
}

这可行,但我想添加某种错误预防验证,我如何使用 SFTP 检查文件夹是否存在?


想出一个解决方案:

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.

所以我可以使用 if 语句来检查 if chdir() === truefalse 以查看目录是否存在.

最佳答案

您可以使用 $sftp->is_dir($location) 来检查它是否是一个目录。

https://stackoverflow.com/questions/22502016/

相关文章:

django - Django 中的弱实体

version-control - Mercurial : Change root of named

google-maps-api-3 - 地理距离 : Wolfram Alpha not agree

indexing - MariaDB 中的索引键大小限制是多少?

cartopy - 在 cartopy 轴之间画线

ssis - 如何使用任何命令行、C# 或 T-SQL 导出部署在 "Integration Ser

sql - 如何从 Rails 中的数据库中选择具有 ID 的不同记录?

sapui5 - 禁用诊断工具快捷键

python - 如何在 Django 中订购反向外键?

authentication - oAuth 2.0 - 代表用户行事