Home >Backend Development >PHP Problem >How to change file permissions in php

How to change file permissions in php

coldplay.xixi
coldplay.xixiOriginal
2020-08-06 11:18:272572browse

How to change file permissions in php: Use the function [chmod] to change the mode of the file specified by filename to that given by mode. The code is [chmod (string $filename, int $mode): bool].

How to change file permissions in php

php method to change file permissions:

chmod instructions and syntax

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

chmod ( string $filename , int $mode ) : bool
  • chmod parameters

  • filename: The path of the file.

  • mode:

Note that mode will not be automatically treated as an octal value, and it cannot use a string (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 值
?>

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
// Read and write for owner, nothing for everybody else
chmod("/somedir/somefile", 0600);
// Read and write for owner, read for everybody else
chmod("/somedir/somefile", 0644);
// Everything for owner, read and execute for others
chmod("/somedir/somefile", 0755);
// Everything for owner, read and execute for owner&#39;s group
chmod("/somedir/somefile", 0750);
?>

chmod return value

Returns TRUE on success, or FALSE on failure.

Related learning recommendations: php programming (video)

The above is the detailed content of How to change file 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