duanhuiqing9528 2015-03-06 22:45
浏览 53
已采纳

PHP和Apache - chown()使用Apache所有者:group?

I'm creating tests for an application that has 3 environments: dev, staging, and production.

I want to run a test which programmatically pulls the default apache owner and group (from httpd.conf), and checks to make sure the uploads directory is owned by this same owner/group combo.

Is there a PHP function that is capable of pulling this data?

  • 写回答

2条回答 默认 最新

  • dppi5167 2015-03-06 23:24
    关注

    Just run the PHP script below.

    Don't forget to change the input data for your setup:
    $apacheEnvVarsConfFile - the apache conf file that defines the user to be used; in this example it was /etc/apache2/envvars and the line defining it was export APACHE_RUN_USER=www-data
    $dirThatYouWantToTest - the directory for which you want to read the user/group permissions

    <?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
    )
    */
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改