首页 > 解决方案 > 实际许可和所有者未打印

问题描述

我正在本地服务器上编写 PHP 文件管理器。
问题是当我去父目录和子目录时,PHP 不会打印实际的权限和所有者。
服务器:Apache2
PHP 版本:7.3.14
我通过单击“..”进入父目录。

请帮我。这是我的代码。

<table>
    <thead>
        <tr>
            <th>Name</th>
            <th>Owner</th>
            <th>Permission</th>
        </tr>
    </thead>
    <tbody>

<?php
    $cwd = getcwd();

    if(isset($_GET['dir'])) {
        $cwd = $_GET['dir'];
    }

    $fil = scandir($cwd);

    foreach ($fil as $file) {
        $path = realpath($file);
        echo '<tr>';
        if(is_dir($file)) {
            echo '<td><a href=" '.$_SERVER['PHP_SELF'].'?dir='.$path.' ">'.$file.'</a></td>';
            echo '<td>'.posix_getpwuid(fileowner($file))[name].'</td>';
            echo '<td>'.substr(sprintf('%o', fileperms($file)), -3).'</td>';
        }

        else {
            echo '<td><a href=" '.$_SERVER['PHP_SELF'].'?edi='.$path.' ">'.$file.'</a></td>';
            echo '<td>'.posix_getpwuid(fileowner($file))[name].'</td>';
            echo '<td>'.substr(sprintf('%o', fileperms($file)), -3).'</td>';
        }
        echo '<tr>';
    }
?>
    </tbody>
</table>

标签: php

解决方案


Apache 用户可能没有读取父目录的权限。您需要确保 Apache 使用的用户具有目录读取和执行权限。Apache 用户通常是 www-data。

请注意:您不想让用户访问他们不应该看到的文件或目录!

您可以使用以下命令更改目录组:

chgrp www-data <directory_name>


推荐阅读