如何在php中获取登录FTP用户的用户组?

时间:2013-09-06 09:08:36

标签: php permissions ftp

我想使用PHP从FTP服务器下载一些文件。现在登录用户无法读取某些文件,我收到此消息:

ftp_get(): Failed to open file.

我知道如何阅读远程文件权限。现在我想检查当前登录的FTP用户是否是文件所有者,以及他是否有权读取该文件。这是我到目前为止(简化):

<?php

$userName = 'vincent';

$conn = ftp_connect($host, $port);
ftp_login($conn, $userName, $password);

// Get user info ($userGroup)
// -> How ???

$list = ftp_rawlist($conn, $directory);

foreach ($list as $file) {

    // -r----x--x 3 vincent vincent 4096 Jul 12 12:16 index.html
    $chunks = preg_split('/\s+/', $file);
    list($item['rights'], $item['number'], $item['user'], $item['group'], $item['size'], $item['month'], $item['day'], $item['time']) = $chunks;
    array_splice($chunks, 0, 8);
    $item['name'] = trim(implode(' ', $chunks));

    if ($item['type'] === 'f') {

        $mode    = substr($item['rights'], 1);
        $trans   = array(
            '-' => 0,
            'r' => 4,
            'w' => 2,
            'x' => 1
        );
        $mode       = strtr($mode, $trans);
        $newmode    = array(0, 0, 0);
        $newmode[0] = $mode[0] + $mode[1] + $mode[2];
        $newmode[1] = $mode[3] + $mode[4] + $mode[5];
        $newmode[2] = $mode[6] + $mode[7] + $mode[8];

        $canRead = false;

        // is logged in user equal file owner and has the owner read-rights?
        if ($userName === $item['user'] && $newmode[0] > 3) {
            $canRead = true;
        }
        // has the logged in user same group and has the file group-read-rights?
        else if ($userGroup === $item['group'] && $newmode[1] > 3) {
            $canRead = true;
        }
        // has the file public-read-rights?
        else if ($newmode[2] > 3) {
            $canRead = true;
        }

        if ($canRead) {
            ftp_get($conn, 'xxx/' . $item['name'], $directory . '/' . $item['name'], FTP_BINARY);
        }
    }
}

0 个答案:

没有答案