PHP chmod function and batch modification of file directory permissions

Source: Internet
Author: User
Tags chmod php tutorial

The chmod () function changes the file mode.Chmod-Changes file mode

TRUE is returned if the call succeeds. Otherwise, FALSE is returned.

Syntax

Chmod (file, mode)

Parameters Description
File Required. Specifies the file to be checked.
Mode

Optional. Specify new permissions.

The mode parameter consists of four numbers:

  • The first digit is always 0.
  • The second number specifies the owner's permissions.
  • The second number specifies the permissions of the user group to which the owner belongs.
  • The fourth digit specifies the permissions of the other owner.

Possible values (if you need to set multiple permissions, Please sum up the following numbers ):

  • 1-execution permission
  • 2-write permission
  • 4-read permission

Let's look at a simple example.

 

<? Php Tutorial
Chmod ("/somedir/somefile", 755); // decimal; probably incorrect
Chmod ("/somedir/somefile", "u + rwx, go + rx"); // string; incorrect
Chmod ("/somedir/somefile", 0755); // octal; correct value of mode
?>

Improved recursive file mode @ infosoft... this is a small short file type that should be processed by all Linux file systems. This allows you to change the permissions of files or directories in batches.

<? Php
Function chmodr ($ path, $ filemode ){
If (! Is_dir ($ path ))
Return chmod ($ path, $ filemode );

$ Dh = opendir ($ path );
While ($ file = readdir ($ dh ))! = False ){
If ($ file! = '.' & $ File! = '..'){
$ Fullpath = $ path. '/'. $ file;
If (is_link ($ fullpath ))
Return FALSE;
Elseif (! Is_dir ($ fullpath )&&! Chmod ($ fullpath, $ filemode ))
Return FALSE;
Elseif (!Chmodr ($ Fullpath, $ filemode ))
Return FALSE;
        }
    }

Closedir ($ dh );

If (chmod ($ path, $ filemode ))
Return TRUE;
Else
Return FALSE;
}
?>

If you have too many directories, you can use

<? Php
$ Iterator = new RecursiveIteratorIterator (new RecursiveDirectoryIterator ($ pathname), RecursiveIteratorIterator: SELF_FIRST );

Foreach ($ iterator as $ item ){
Chmod ($ item, $ filemode );
}
?>

This code is used to modify the directory permissions.

Haha, we are not only talking about the simple chmod syntax, but also doing complicatedChmodUse instance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.