Hongmu Notes
Home Language Notes Set directory access permissions for PHP; synchronize settings for all files within a directory; function.
Language Notes PHP

Set directory access permissions for PHP; synchronize settings for all files within a directory; function.

Set directory access permissions for PHP; synchronize settings for all files within a directory; function.

You can use the `chmod` function in PHP to modify the permissions for directories and files. This function requires two parameters: the target path and the desired permissions. Permissions are represented as three-digit numbers, where each digit corresponds to the permissions granted to the owner, the group, and other users, respectively.

For example, the following code sets the permissions for the owner, group, and other users of the target directory to read, write, and execute permissions:

chmod('/path/to/directory', 0777);

Please note that granting the 777 permission allows anyone to access this directory and its files, which could pose security risks. To protect your files, it is recommended to grant only the necessary permissions.

If you need to recursively modify the permissions for a directory and all its files, consider using a recursive function. Here is an example function:

/**
 * 递归更改目录及其子目录和文件的权限
 *
 * @param string $path 要更改权限的目录路径
 * @param int $filemode 文件权限(例如:0644)
 * @param int $dirmode 目录权限(例如:0755)
 *
 * @return bool 成功则返回 true,否则返回 false
 */
function chmod_r($path, $filemode, $dirmode) {
    if (is_dir($path) ) {
        if (!chmod($path, $dirmode)) { // 更改目录的权限为 dirmode
            return false;
        }
        $dh = opendir($path);
        while (($file = readdir($dh)) !== false) {
            if($file != '.' && $file != '..') {
                $fullpath = $path.'/'.$file;
                if(is_link($fullpath)) {
                    if (!chmod($fullpath, $filemode)) { // 更改符号链接文件的权限为 filemode
                        return false;
                    }
                } elseif(!is_dir($fullpath)) {
                    if (!chmod($fullpath, $filemode)) { // 更改普通文件的权限为 filemode
                        return false;
                    }
                } elseif(!chmod_r($fullpath, $filemode, $dirmode)) { // 递归更改子目录和文件的权限
                    return false;
                }
            }
        }
        closedir($dh);
    } else {
        if (!chmod($path, $filemode)) { // 更改目录下文件的权限为 filemode
            return false;
        }
    }
    return true; // 成功返回 true
}

You can use this function to modify the permissions of the target directory, as well as the permissions of all subdirectories and files within that directory. For example, the following code sets the permissions for the target directory to 755 and sets the permissions for all files within the directory to 644:

chmod_r('/path/to/directory', 0644, 0755);
微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

PHP ob function record

PHP ob function record Language Notes PHP

Usage of the following three functions ob_get_contents(); ob_end_clean(); ob_start(); You can use these functions to buffer local files and execute local script code. Use ob_start() to save the output code into the buffer, and the page will not be displayed; then use ob_get_contents to get the data in the buffer. o…
👁 141
PHP preg

PHP preg Language Notes PHP

The preg_match_all function is used to perform a global regular expression match. preg_match_all() Syntax int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG…
👁 169
Detailed explanation of PHP ternary operator and if

Detailed explanation of PHP ternary operator and if Language Notes PHP

Ternary operator condition ? Result 1 : Result 2 Explanation: The position in front of the question mark is the condition for judgment. If the condition is met, the result is 1, and if it is not met, the result is 2. This article compares and explains the ternary operator and if...else... in detail. I hope it will be helpful to everyone. Today when I was revising my paper online, I encountered a statement that I couldn’t understand: $if_summary = $row['IF_SUMMARY']=…
👁 189
PHP cast type

PHP cast type Language Notes PHP PHP collection PHP and mysql

Get the data type 1. If you want to check the value and type of an expression, use var_dump(). 2. If you just want to get an easy-to-read type expression for debugging, use gettype(). 3. To check a certain type, do not use gettype(), but use the is_type() function. Converting Strings to Numbers When a string is evaluated as a number, the result is determined according to the following rules...
👁 232

Recommended reading

Atmospheric Photography Studio Website Template 0950

Atmospheric Photography Studio Website Template 0950 Practical Collection Yiyou template

An EyouCMS website template designed for professional photography studios. Its visual art-inspired design is perfect for showcasing photographic works, studio environments, photography services, and brand stories. This template helps photography studios present a premium online presence and attract clients. Template Demo | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS (E...
👁 54
Household Care and Postpartum Care Service Website Template 0142

Household Care and Postpartum Care Service Website Template 0142 Practical Collection Yiyou template

An EyouCMS website template designed for the domestic services, postpartum care, and nanny service industries. Its warm yet professional design is ideal for showcasing domestic services, postpartum care and nanny services, cleaning services, as well as staff profiles – helping domestic service providers attract household clients online. Template Overview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS (Ey...
👁 62
(Adaptive mobile version) Responsive eco-friendly technology company website – PBootCMS template; HTML5 blue eco-friendly machinery and equipment website source code download – 0531

(Adaptive mobile version) Responsive eco-friendly technology company website – PBootCMS template; HTML5 blue eco-friendly machinery and equipment website source code download – 0531 Practical Collection pbootcms Template

A responsive website template developed by the eco-tech company PbootCMS, compatible with both PC and WAP devices. Its modern, eco-friendly design is ideal for showcasing environmental protection equipment, technical solutions, and sustainable development concepts. This template helps environmental technology enterprises build their online brand presence and attract both government and corporate clients. Template Preview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5....
👁 40
Adobe After Effects 2021 SP

Adobe After Effects 2021 SP Hobbies video clip

I have been playing around with ae video editing recently, and downloaded several ae, but none of them were successful. Finally, I found a 2021 version of the ae software, and I will use it for the time being. I will record it here. I feel like I will download it in the future, so I’ll record it so I can’t find it later. Download link: 🔒 Hidden content: View https://pan.baidu.com/s/1xYz7d9YxAdyNU8Zre2KYrA?pwd=g2n…
👁 577
Responsive wedding photography website template – 0951

Responsive wedding photography website template – 0951 Practical Collection Yiyou template

This EyouCMS responsive template is ideal for the wedding and travel photography industry. Its romantic and elegant design makes it perfect for showcasing wedding travel photography portfolios, photography services, travel destinations, and client testimonials. It helps wedding photography agencies attract prospective clients online. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS...
👁 32
Self-Media Operations Website Template 0143

Self-Media Operations Website Template 0143 Practical Collection Yiyou template

This EyouCMS template is ideal for self-media management websites, featuring a clean and modern design perfect for content creators to share articles, videos, and operational insights. It helps self-media creators build their personal online brand and attract followers. Template Preview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS (EyouCMS)...
👁 33