chmod() function in PHP


The chmod() function changes the file mode. It returns TRUE on success and FALSE on failure.

Syntax

chmod($file_path, file_mode)

Parameters

  • file_path − Set the path of file or directory to be checked for existence. Required.

  • file_mode − Set the mode with values. The description of file_mode parameter is shown below

File Mode parameter

Set the file mode with the following four values.

  • zero
  • permission for owner
  • permission for the owner’s user group
  • permissions for rest

The following are the values to set multiple permissions. You need to add the following numbers −

  • 1 = execute permissions
  • 2 = write permissions
  • 4 = read permissions

Return

The file_exists() method returns.

  • True, on success
  • False, on failure

Example

The following is an example that changes the mode for file “one.txt”. This sets read and write permission for owner, nothing for everybody else.

<?php
   // Setting mode for file
   // Read and write permission for owner, nothing for everybody else
   chmod("one.txt",0600);
?>

Let us see another example that changes the file mode for “two.txt”. This sets read and write permission for owner, read for everybody else.

<?php
   // Setting mode for file
   // Read and write permission for owner, read for everybody else
   chmod("two.txt",0644);
?>

Let us see another example that changes the file mode for “three.txt”. This sets all the permissions for owner, read and execute for everybody else.

<?php
   // Setting mode for file
   // All the permissions for owner, read and execute for everybody else
   chmod("three.txt",0755);
?>

Let us see another example that changes the file mode for “four.txt”. This sets all the permissions for owner, read for owner's group.

<?php
   // Setting mode for file
   // All the permissions for owner, read for owner's group
   chmod("four.txt",0740);
?>

Let us see another example that changes the file mode for “five.txt”. This sets all the permissions for owner, read and execute for owner's group.

<?php
   // Setting mode for file
   // All the permissions for owner, read and execute for owner's group
   chmod("five.txt",0740);
?>

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 24-Jun-2020

358 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements