dream989898 2019-04-30 01:06
浏览 149
已采纳

如何使用PHP通过FTP从文件获取chmod权限

I am working on something which uses FTP but I need to get the existing chmod permissions of each file. How can I achieve this?

I have tried the fileperms() function, but that seems to only work with local files.

  • 写回答

1条回答 默认 最新

  • dqwh1203 2019-04-30 05:14
    关注

    If you are using PHP 7.2 and newer and your FTP server supports MLSD command, it's easy, as you can use ftp_mlsd function.

    $conn_id = ftp_connect("ftp.example.com") or die("Cannot connect");
    ftp_login($conn_id, "username", "password") or die("Cannot login");
    ftp_pasv($conn_id, true) or die("Cannot change to passive mode");
    
    $entries = ftp_mlsd($conn_id, "/remote/path") or die("Cannot list directory");
    
    foreach ($entries as $entry)
    {
        if (($entry["type"] != "cdir") && ($entry["type"] != "pdir"))
        {
            echo $entry["name"] . " - " . $entry["UNIX.mode"] . "
    ";
        }
    }
    

    If not, you have to use LIST command using ftp_rawlist function and parse a proprietary format that the server returns.

    The following code assumes a common *nix format.

    $entries = ftp_rawlist($conn_id, "/remote/path") or die("Cannot list directory");
    
    foreach ($entries as $entry)
    {
        $tokens = explode(" ", $entry);
        $name = $tokens[count($tokens) - 1];
        $permissions = $tokens[0];
        echo $name . " - " . $permissions . "
    ";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 matlab ode45 未发现警告,但是运行出错
  • ¥15 设计一个光控计数器,全部用ttl芯片
  • ¥15 vscode platformio
  • ¥15 代写uni代码,app唤醒
  • ¥15 全志t113i启动qt应用程序提示internal error
  • ¥15 ensp可以看看嘛.
  • ¥80 51单片机C语言代码解决单片机为AT89C52是清翔单片机
  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决