PHP Function Reference

PHP fileperms() Function



The PHP fileperms() function returns permissions for the given file.

Note: The results of this function are cached. Use clearstatcache() function to clear the cache.

Syntax

fileperms(filename)

Parameters

filename Required. Specify the path to the file to check.

Return Value

Returns the file's permissions as a numeric mode. Lower bits of this mode are the same as the permissions expected by chmod(), however on most platforms the return value will also include information on the type of file also.

Returns false on failure.

Exceptions

Upon failure, an E_WARNING is emitted.

Example: fileperms() example

Lets assume that we have a file called test.txt in the current working directory. The example below describes how to use fileperms() function to get the permissions set for this file.

<?php
$file = "test.txt";
  
//displaying permissions set for the file
echo fileperms($file)."\n";
?>

The output of the above code will be:

33188

Example: displaying permissions in octal format

The following program can be used to display the permissions in octal format.

<?php
$file = "test.txt";
  
//displaying permissions set for the file 
//in octal format 
echo sprintf("%o", fileperms($file))."\n";

//displaying permissions set for the file 
//in octal format - considering only last 
//four characters (removing mode file types)
echo substr(sprintf("%o", fileperms($file)), -4);
?>

The output of the above code will be:

100644
0644

❮ PHP Filesystem Reference