W3docs

fileperms()

The fileperms() function is a built-in PHP function that returns the permissions of a file. This function returns the permissions as an integer, which

What is the fileperms() Function?

The fileperms() function is a built-in PHP function that returns the permissions of a file. This function returns the permissions as a decimal integer representing the Unix file mode. To view the permissions in octal notation, you can use sprintf('%o', fileperms($filename)).

Here's the basic syntax of the fileperms() function:

The PHP syntax of fileperms()

fileperms(filename);

Where filename is the name of the file to be checked.

How to Use the fileperms() Function?

Using the fileperms() function is straightforward. Here are the steps to follow:

  1. Call the fileperms() function, passing in the name of the file you want to check.
  2. The function will return the permissions of the file as a decimal integer. Use sprintf('%o', ...) to convert it to octal notation for easier reading.

Here's an example code snippet that demonstrates how to use the fileperms() function:

<?php

$filename = 'myfile.txt';

if (file_exists($filename)) {
    $permissions = fileperms($filename);
    echo "The file $filename has permissions " . sprintf('%o', $permissions);
} else {
    echo "File not found.";
}

In this example, we check if the file myfile.txt exists to prevent warnings, then retrieve its permissions. The sprintf('%o', $permissions) call converts the decimal Unix mode into a standard octal string (e.g., 0644 for regular files or 0755 for directories). You can interpret the octal digits as owner, group, and other permissions respectively.

Conclusion

The fileperms() function is a useful tool in PHP for checking the permissions of a file. By following the steps outlined in this guide, you can easily use the fileperms() function in your PHP projects to check the permissions of files.

Practice

Practice

Which of the following statements about PHP file permissions are true?