dream989898
dream989898
2019-04-30 01:06

如何使用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 dqwh1203 2年前

    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 . "
    ";
    }
    
    点赞 评论 复制链接分享