Categories
PHP

File Permission

Learn how to use the is_executable(), is_readable(), is_writable(), fileperms(), and chmod() functions to determine whether your PHP script has permission to execute, read or write a particular file.

To determine whether your PHP script has permission to execute, read or write a particular file, use the following functions:

is_executable()

<?php
 //Syntax
 is_executable(string $filename): bool

The is_executable() function returns true if the given filename exists and is executable, otherwise, returns false.

Example: Determine whether the file is executable:

<?php
 // Unix based OSs
 if ( is_executable('file.sh') )
  echo 'executable';
 else
  echo 'not executable';

 // Windows OS
 if ( is_executable('D:/xampp/php/php.exe') )
  echo 'php.exe is executable';
 else
  echo 'php.exe is not executable';

is_readable()

<?php
 //Syntax
 is_readable(string $filename): bool

The is_readable() function returns true if file exists and is readable, otherwise, returns false.

<?php
 if is_readable('file.txt')
  echo 'readable';
 else
  echo 'Not readable';

Example: open a file in read mode if it is readable

<?php
 $file = 'file.txt';
 if (is_readable($file)){
  $fp = fopen($file, 'r');
  if ($fp !== false) {
   while (!feof($fp)) {
    $line = fgets($fp);
    echo $line.'<br>';
   }
  }
 }

is_writable()

<?php
 //Syntax
 is_writable(string $filename): bool

 //Alias of is_writable()
 is_writeable(string $filename): bool)

The is_writable() (or is_writeable()) function returns true if file exists and is writable, otherwise, returns false.

<?php
 if is_writable('file.txt')
  echo 'Writable';
 else
  echo 'Not writable';

Example: open a file in write mode if it is writable

<?php
 $file = 'file.txt';
 if (is_writable($file)){
  $fp = fopen($file, 'w');
  if ($fp !== false) {
   fwrite($fp, "First line.\nSecond line");
  }
 }

fileperms()

<?php
 //Syntax
 fileperms(string $filename): int|false

You can use the fileperms() function to get the permissions values set on a file or dir. This function returns the permissions in numeric form, to convert the returned numeric value into an octal value you might use decoct() or sprintf() function:

Example: Get file/dir permissions with fileperms()

<?php
 $num = fileperms( 'file.sh' ); // 33206
 $oct = decoct( $num ); // 100666
 //$oct = sprintf('%o', $num);

 // last four digits represent permissions
 $perm = substr( $oct, -4 );
 echo $perm;
 //Prints: 0666

Example: Get file/dir permissions with stat()

<?php
 $stat = stat( 'file.sh' );
 $oct = decoct( $stat['mode'] );
 echo substr( $oct, -4 ); // 0666

The substr( $oct, -4 ) line in the above code returns the last four digits from the octal value, these four digits represent the permissions set on the given file.

Example: get file permissions – one-liner code

<?php
 echo substr(decoct(fileperms('file.sh')), -4);

 // OR

 echo substr(sprintf('%o', fileperms('file.sh')), -4);

For more information visit https://php.net/manual/function.fileperms.php.

chmod – change file permissions

<?php
 //Syntax
 chmod(string $filename, int $permissions): bool

To change the permissions of a file or directory, use the chmod() function. This function returns true if the permission change was successful, and false if it failed.

To change a file’s permissions provide the filename and the new permission.

Example: Set a file’s permission to 644

<?php
 $modChanged = chmod( 'file.txt', 0644 );
 if ( $modChanged )
  echo 'Permission changed';
 else
  echo 'Permission not changed';

 //Prints: Permission changed

Note: The 0 before the 644 tells PHP to interpret the digits as an octal number.

See, https://php.net/manual/function.chmod.php.


Working with Files in PHP: