为什么php脚本没有写入权限

问题描述 投票:0回答:0

我写了一个简单的 php 脚本来检查 php 进程是否具有写入特定文件夹的权限。

<!-- check if apache has write permission in specific folder -->
<?php
$folder = '/var/www/html/easyappointments/storage/';
// $folder = '/tmp/';
$testfile = '.testfile';
// get whoami
$whoami = `whoami`;
//echo whoami and start a new line
echo "the user running the PHP script is : $whoami <BR>";

//check what permissions the folder has
$perms = fileperms($folder);
//convert the permissions to octal
$perms = substr(sprintf('%o', $perms), -4);
echo "The folder $folder has permissions in octal: $perms <BR>";

//tell who is the owner of the folder
$owner = fileowner($folder);
//what is the real name of the owner
$owner = posix_getpwuid(fileowner($folder));
echo "The folder $folder is owned by: $owner[name] <BR>";

if (is_writable($folder)) {
    if ($fp = fopen($folder . $testfile, 'w')) {
        echo "The file $folder$testfile is writable";
        fclose($fp);
        unlink($folder . $testfile);
    } else {
        echo "The file $folder$testfile is not writable";
    }
} else {
    //echo why the folder is not writable
    echo "The folder $folder is not writable because the permissions are: $perms <BR>";
}


在chrome浏览器中运行后,以上脚本的输出为

#####################

运行 PHP 脚本的用户是:apache

文件夹/var/www/html/easyappointments/storage/八进制权限:0777

文件夹 /var/www/html/easyappointments/storage/ 属于:apache

文件夹/var/www/html/easyappointments/storage/不可写,因为权限是:0777

#####################

我完全不明白为什么“apache”(运行 PHP 脚本)不能写入“apache”拥有的一个文件夹。

我的环境如下:

  1. 森图斯 7
  2. PHP 7.3.33
  3. 阿帕奇 2.4.6

如何确保我的 PHP 脚本具有写入一个特定文件夹的权限。如何授予文件夹权限?

php apache centos7
© www.soinside.com 2019 - 2024. All rights reserved.