PHP Tutorial - PHP chmod() Function






Definition

The chmod() function changes permissions of the specified file.

Syntax

PHP chmod() Function has the following syntax.

chmod(file,mode)

Parameter

ParameterIs RequiredDescription
fileRequired.File to check
modeRequired.New permissions.

The mode parameter consists of four numbers:

  • The first number is always zero
  • The second number specifies permissions for the owner
  • The third number specifies permissions for the owner's user group
  • The fourth number specifies permissions for everybody else

Possible values (to set multiple permissions, add up the following numbers):

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




Returns

Returns TRUE on success and FALSE on failure.

Example


<?PHP/* w w w .  ja va2s  .  co  m*/
//sets the file to readable, writable, and executable by all users
chmod("/var/www/myfile.txt", 0777);

//sets the file to readable, writable, and executable by owner, 
//and just readable and writable by everyone else.

chmod("/var/www/myfile.txt", 0755);

// Read and write for owner, nothing for everybody else
chmod("test.txt",0600);

// Read and write for owner, read for everybody else
chmod("test.txt",0644);

// Everything for owner, read for owner's group
chmod("test.txt",0740);

?>