douyi9447 2013-09-06 09:08
浏览 29

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

I want to download some files from a FTP server using PHP. Now some files are not readable by the logged in user and I get this message:

ftp_get(): Failed to open file.

I know how to read the remote file permissions. Now I want to check if the currently logged in FTP user is the file owner and if he has the right to read that file. This is what I got so far (simplified):

<?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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大