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 . "
    ";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料