PHP & Apache - 使用 Apache owner:group 的 chown()?

PHP & Apache - chown() using Apache owner:group?

我正在为具有 3 个环境的应用程序创建测试:开发环境、暂存环境和生产环境。

我想 运行 以编程方式拉取默认的 apache 所有者和组(来自 httpd.conf)的测试,并检查以确保上传目录属于同一 owner/group连击

是否有能够提取此数据的PHP函数?

这取决于 php 脚本 运行 的权限。如果它 运行 是默认的 apache 用户,它可以。使用任何读取文件的 php 函数,例如 file_get_contents 或 parse_ini_file.

但是,如果 php 线程无法访问 apache 文件夹(例如 运行s 在不同的权限下),那么获取数据就很棘手。从某种意义上说,您将不得不通过 php 破解 apache。但是由于您拥有服务器,您可以 php 更改用户或调用外部脚本(这将 运行 在其他权限下)并通过外部脚本获取数据

只是 运行 下面的 PHP 脚本。

不要忘记为您的设置更改输入数据:
$apacheEnvVarsConfFile - 定义要使用的用户的 apache conf 文件;在这个例子中它是 /etc/apache2/envvars 并且定义它的行是 export APACHE_RUN_USER=www-data
$dirThatYouWantToTest - 您要读取其 user/group 权限的目录

<?php

## define input data
$apacheEnvVarsConfFile = '/etc/apache2/envvars';
$dirThatYouWantToTest = '/var/www';

## compute the information that is needed
$data = array();

$data['apacheUser'] = str_replace('export APACHE_RUN_USER=', '', exec('cat '.$apacheEnvVarsConfFile.' | grep APACHE_RUN_USER'));

$dirStat = stat($dirThatYouWantToTest);
$data['dirTested'] = $dirThatYouWantToTest;
$data['dirUser'] = posix_getpwuid($dirStat[4])['name'];
$data['dirGroup'] = posix_getgrgid($dirStat[5])['name'];
$data['dirPerms'] = substr(sprintf('%o', fileperms($dirThatYouWantToTest)), -4);

echo '<pre>';print_r( $data );echo '</pre>';

## the code above will produce the following output (example): 
/*
Array
(
    [apacheUser] => www-data
    [dirTested] => /var/www
    [dirUser] => john
    [dirGroup] => john
    [dirPerms] => 0755
)
*/