How to modify file permissions in php?

青灯夜游
Release: 2023-03-07 11:32:01
Original
2993 people have browsed it

In PHP, you can use the built-in function chmod() to modify the permissions of the file; this function can change the permissions of the specified file. The syntax format is "chmod(file,mode)". If the modification is successful, it returns TRUE. , and returns FALSE if it fails.

How to modify file permissions in php?

Recommended: "PHP Video Tutorial"

PHP chmod() function can change the permissions of the specified file; if Returns TRUE on success and FALSE on failure.

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

chmod(file,mode)
Copy after login

Parameters:

How to modify file permissions in php?

Note: mode will not be automatically treated as an octal value, and it cannot use strings (such as " g w"). 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 值
?>
Copy after login

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. For information about file permissions on UNIX systems, read the manuals "man 1 chmod" and "man 2 chmod".

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

For more programming-related knowledge, please visit: Programming Video! !

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

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!