如何更改用户所有者,以便可以通过FTP编辑和/或删除使用PHP脚本创建的文件夹


How can I change the user owner so a folder created with a PHP script can be edited and/or deleted through FTP?

我们有一个小CMS,我们可以在其中为特定内容创建文件夹。在我们搬到新服务器之前,一切都运行良好;我注意到在新服务器上,PHP(CMS操作)中的"用户"与FTP用户不同。这在以前没有发生过,但现在每次通过PHP脚本创建新文件夹时,都无法通过FTP进行修改。

我注意到PHP文件夹的所有者是99 99,我认为这是"没有人"。

我需要做什么才能让它像在以前的服务器中一样工作?在我的 PHP 脚本上,权限设置为 0777,所以我真的不知道还能做什么。

我可以访问SSH,WHM和cpanel,也许PHP设置或APACHE设置中有一些东西可以修改,也许可以直接通过WHM。

请,

如果命令外壳上有什么工作要做,请逐步描述它,因为我对 shell 脚本一无所知。

这是我在 PHP 中用来创建文件夹的代码:

$year_folder = date(Y)."/";
$user_folder = $year_folder."$alias/";
$section_folder = $user_folder."$section/";
//CREATE YEAR FOLDER and persmissions (Forbiden to access directly)
if (!file_exists($year_folder)) {
    mkdir("$year_folder", 0711);// create directory
}
//CREATE USER FOLDER
if (!file_exists($user_folder)) {
    mkdir("$user_folder", 0777);// create directory
}           
    //CREATE CLASS FOLDER
if (!file_exists($section_folder)) {
    mkdir("$section_folder", 0777);
}

谢谢!

您可以尝试 chown (http://php.net/manual/en/function.chown.php),但这取决于服务器是否允许 Web 服务器修改文件。

首先,切勿将文件设置为 0777。永远不需要它。如果应用程序告诉您这样做,您应该重新考虑该应用程序的实现,因为可能存在危险的安全漏洞。

Apache在哪个用户下运行?哪些用户需要访问此文件夹?

其次,使用 chmod 或 chgrp。

最有可能的,也是更安全的方法之一,但不是唯一的方法,是使用 chgrp 将文件夹分配给需要访问权限的一个或多个用户组。

如果向所有者和组(例如 760 或 770 等)授予读/写访问权限,并将其他对象保持在 0,则可以保护应用程序,同时仍允许自己和组中的其他人访问。

您是否愿意编辑创建文件夹以添加 chgrp 或 chmod 命令的 PHP CMS 脚本?更新

您可以使用递

归执行一次,而不是创建目录三次:

$year_folder = date(Y)."/";
$user_folder = $year_folder."$alias/";
$section_folder = $user_folder."$section/";
//CREATE Folder
if (!is_dir($section_folder)) { // changed to is_dir to keep syntax within context
    if(!mkdir($section_folder, 0764, true)){
        echo "Failed to create directory: $section_folder";
    }
}

作为调试步骤,您可以在上述代码后添加以下内容来检查权限:

$perms = fileperms($section_folder);
if (($perms & 0xC000) == 0xC000) {
    // Socket
    $info = 's';
} elseif (($perms & 0xA000) == 0xA000) {
    // Symbolic Link
    $info = 'l';
} elseif (($perms & 0x8000) == 0x8000) {
    // Regular
    $info = '-';
} elseif (($perms & 0x6000) == 0x6000) {
    // Block special
    $info = 'b';
} elseif (($perms & 0x4000) == 0x4000) {
    // Directory
    $info = 'd';
} elseif (($perms & 0x2000) == 0x2000) {
    // Character special
    $info = 'c';
} elseif (($perms & 0x1000) == 0x1000) {
    // FIFO pipe
    $info = 'p';
} else {
    // Unknown
    $info = 'u';
}
// Owner
$info .= (($perms & 0x0100) ? 'r' : '-');
$info .= (($perms & 0x0080) ? 'w' : '-');
$info .= (($perms & 0x0040) ?
            (($perms & 0x0800) ? 's' : 'x' ) :
            (($perms & 0x0800) ? 'S' : '-'));
// Group
$info .= (($perms & 0x0020) ? 'r' : '-');
$info .= (($perms & 0x0010) ? 'w' : '-');
$info .= (($perms & 0x0008) ?
            (($perms & 0x0400) ? 's' : 'x' ) :
            (($perms & 0x0400) ? 'S' : '-'));
// World
$info .= (($perms & 0x0004) ? 'r' : '-');
$info .= (($perms & 0x0002) ? 'w' : '-');
$info .= (($perms & 0x0001) ?
            (($perms & 0x0200) ? 't' : 'x' ) :
            (($perms & 0x0200) ? 'T' : '-'));
echo $info;

以上应呼应:

-rwxrw--r--