Home >Backend Development >PHP Problem >How to modify file read and write permissions in php

How to modify file read and write permissions in php

青灯夜游
青灯夜游Original
2021-05-24 17:52:563500browse

In PHP, you can use the chmod() function to modify the file read and write permissions. The function of this function is to change the permissions of the specified file; the syntax format is "chmod(file,mode)", and the parameter file specifies the Check the file, the parameter mode specifies new permissions.

How to modify file read and write permissions in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php changes the read and write permissions of the file

<?php
// 所有者可读写,其他人没有任何权限
chmod("test.txt",0600);
// 所有者可读写,其他人可读
chmod("test.txt",0644);
// 所有者有所有权限,其他所有人可读和执行
chmod("test.txt",0755);
// 所有者有所有权限,所有者所在的组可读
chmod("test.txt",0740);
?>

Related function description:

chmod() function changes the permissions of the specified file. Returns TRUE if successful, FALSE if failed.

Syntax

chmod(file,mode)

chmod will try to change the mode of the file specified by file to that given by mode.

Parameters Description
file Required. Specifies the documents to be checked.
mode Required. Specify new permissions.

The mode parameter consists of 4 numbers:

  • The first number is usually 0
  • The second number specifies the owner's permissions
  • The third The first number specifies the permissions of the user group to which the owner belongs
  • The fourth number specifies the permissions of everyone else

Possible values ​​(if you need to set multiple permissions, please set the following Total the numbers):

  • 1 = Execute permission
  • 2 = Write permission
  • 4 = Read permission

Note: mode will not be automatically treated as an octal value, and strings (such as "g w") cannot be used. To ensure correct operation, you need to add 0 in front of the mode:

<?php
chmod("/somedir/somefile", 755);   // 十进制数,可能不对
chmod("/somedir/somefile", "u+rwx,go+rx"); // 字符串,不对
chmod("/somedir/somefile", 0755);  // 八进制数,正确的 mode 值
?>

The mode parameter contains three octal numbers in order to specify the owner, the owner's group, and everyone's access restrictions. Each part can be calculated by adding the required permissions. The number 1 makes the file executable, the number 2 makes the file writable, and the number 4 makes the file readable. Add these numbers to specify the required permissions.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to modify file read and write permissions in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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