Home Backend Development PHP Tutorial PHP mkdir()无写权限的问题解决方法_PHP

PHP mkdir()无写权限的问题解决方法_PHP

Jun 01, 2016 am 11:50 AM
php

使用mkdir创建文件夹时,发现这个函数有两个参数,第二个参数是为新创建的文件夹指定权限。

但是如果直接用mkdir('文件地址', 0777);时 发现新文件夹的权限并不是777,一般情况下会是022。

因为mkdir在给文件夹制定权限时,会跟当前登录操作系统用户的umask(用户缺省权限属 性)值进行位“与”,得到的值才是最终权限值。

umask 是什么?

我们创建文件的默认权限是怎么来的?如何改变这个默认权限呢?

当我们登录系统之后创建一个文件总是有一个默认权限的,那么这个权限是怎么来的呢?这就是 umask 干的事情。

umask 设置了用户创建文件的默认权限,它与 chmod 的效果刚好相反,umask 设置的是权限“补码”,而 chmod 设置的是文件权限码。一般在 /etc/profile、$HOME/.bash_profile 或 $HOME/.profile 中设置 umask 值。

如何计算 umask 值?

umask 命令允许你设定文件创建时的缺省模式,对应每一类用户(文件属主、同组用户、其他用户)存在一个相应的 umask 值中的数字。对于文件来说,这一数字的最大值分别是 6。系统不允许你在创建一个文本文件时就赋予它执行权限,必须在创建后用 chmod 命令增加这一权限。目录则允许设置执行权限,这样针对目录来说,umask 中各个数字最大可以到 7。

该命令的一般形式为:umask nnn,其中 nnn 可为 000 - 777。

我们只要记住 umask 是从权限中“拿走”相应的位即可。

如:umask 值为 022,则默认目录权限为 755,默认文件权限为 644。
所以,如果用户umask是022(一般默认是这个),即:000 010 010 在于mkdir指定的777 ,即:111 111 111 位“与”后,得到的真实权限为:022。

如果想让新建文件夹权限最大,有两种方法可以实现:(当然,是在当前用户能赋予最高权限的条件下)

1、修改用户umask,php提供有umask函数:

复制代码 代码如下:
$oldumask=umask(0);
mkdir('test',0777);
umask($oldumask);

这种方法看起来一劳永逸,在脚本开头文件里指定下umask值,后面直接用mkdir就可以控制权限,需要注意的是:在多线程服务器上使用umask函数时,多个线程会公用一个umask,所以可能会造成混乱。

2、使用chmod函数,这也是最常用的方法:
复制代码 代码如下:
mkdir('文件地址', 0777);
chmod('文件地址', 0777);

最后,需要注意一点,权限值最好使用八进制表示,即“0”开头,而且一定不要加引号。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles