File Permissions


File Permissions Interview with follow-up questions

1. What are file permissions in PHP and why are they important?

File permissions in PHP (and Unix/Linux systems generally) determine who can read, write, or execute files and directories. PHP scripts often need to manage permissions when handling uploaded files, creating log directories, or setting up deployment configurations.

Why file permissions matter for PHP:

  • Security: Overly permissive files (777) allow any process or user on the server to read, modify, or execute them — a major security risk, especially for configuration files containing credentials.
  • Functionality: PHP cannot write to an upload directory without write permission. Files set without execute permission cannot be run.
  • Least privilege principle: Each file and directory should have the minimum permissions required for its purpose.

Typical permission settings:

  • PHP source files: 644 (owner read/write, group read, others read)
  • Upload directories: 755 (owner read/write/execute, others read/execute)
  • Configuration files with credentials: 640 or 600 (readable only by owner/group)
  • CLI scripts that need to be executed: 755

PHP functions for working with permissions:

  • chmod() — change permissions
  • fileperms() — read current permissions
  • chown() — change owner (requires elevated privileges)
  • is_readable(), is_writable(), is_executable() — check permission status

PHP process context: PHP scripts run as the web server user (e.g., www-data on Debian/Ubuntu). Files created by PHP are owned by that user. Ensure the web server user has appropriate access to upload directories but not to sensitive configuration files.

↑ Back to top

Follow-up 1

Can you explain the different types of file permissions?

In PHP, there are three types of file permissions:

  1. Read (r): Allows a user to view the contents of a file.
  2. Write (w): Allows a user to modify or delete a file.
  3. Execute (x): Allows a user to execute a file as a program or script.

These permissions can be assigned to three different entities: the owner of the file, the group the file belongs to, and others (everyone else). Each entity can have different permissions assigned to them.

Follow-up 2

How do you change file permissions in PHP?

In PHP, you can change file permissions using the chmod() function. The chmod() function takes two arguments: the path to the file or directory, and the new permissions in numeric format.

Here's an example of how to change file permissions to allow read and write access for the owner and read-only access for the group and others:


Follow-up 3

What are the potential security risks if file permissions are not set correctly?

If file permissions are not set correctly in PHP, it can lead to various security risks:

  1. Unauthorized access: If file permissions are too permissive, anyone can read, modify, or delete sensitive files, leading to unauthorized access to sensitive information.
  2. Code execution: If file permissions allow execution by unauthorized users, it can lead to the execution of malicious code or scripts.
  3. Data integrity: Incorrect file permissions can result in accidental modification or deletion of important files, leading to data loss.

It is important to set file permissions carefully to mitigate these risks.

2. How can you check the file permissions of a file in PHP?

Use fileperms() to retrieve the numeric permissions of a file, then format the result for readability:

$filename = '/var/www/app/upload/avatar.jpg';

// Get permissions as an integer
$perms = fileperms($filename);

// Format as octal string (e.g., "0644")
$octal = substr(sprintf('%o', $perms), -4);
echo $octal; // "0644"

// Check specific permission bits
$isReadable   = is_readable($filename);
$isWritable   = is_writable($filename);
$isExecutable = is_executable($filename);

echo "Readable: " . ($isReadable ? 'Yes' : 'No') . "\n";
echo "Writable: " . ($isWritable ? 'Yes' : 'No') . "\n";

Formatted octal display (like ls -l):

$info = '';
$info .= (($perms & 0x0100) ? 'r' : '-'); // owner read
$info .= (($perms & 0x0080) ? 'w' : '-'); // owner write
$info .= (($perms & 0x0040) ? 'x' : '-'); // owner execute
$info .= (($perms & 0x0020) ? 'r' : '-'); // group read
$info .= (($perms & 0x0010) ? 'w' : '-'); // group write
$info .= (($perms & 0x0008) ? 'x' : '-'); // group execute
$info .= (($perms & 0x0004) ? 'r' : '-'); // others read
$info .= (($perms & 0x0002) ? 'w' : '-'); // others write
$info .= (($perms & 0x0001) ? 'x' : '-'); // others execute
echo $info; // e.g., "rw-r--r--"

Note: fileperms() results are cached by PHP's stat cache. If you change permissions and check again in the same script, call clearstatcache() first.

↑ Back to top

Follow-up 1

What function is used to check file permissions?

The fileperms() function is used to check file permissions in PHP.

Follow-up 2

What does the output of this function mean?

The output of the fileperms() function is an integer representing the file permissions in octal format. Each digit in the octal number represents the permission for a specific user group (owner, group, others).

Follow-up 3

Can you provide a code example of checking file permissions?

Sure! Here's an example of how to check the file permissions of a file in PHP:

$file = 'path/to/file.txt';
$permissions = fileperms($file);
echo 'File permissions: ' . decoct($permissions);

In this example, we first specify the path to the file we want to check. Then, we use the fileperms() function to get the file permissions as an integer. Finally, we use the decoct() function to convert the integer to octal format and display the result.

3. What is the difference between 'read', 'write' and 'execute' permissions in PHP?

On Unix/Linux systems (where PHP is most commonly deployed), permissions are assigned separately for three groups:

  • Owner — the user who owns the file
  • Group — a Unix group associated with the file
  • Others — everyone else

For each group, three permission bits apply:

Read (r / 4)

  • File: Can view the file's contents
  • Directory: Can list the directory's contents (ls)

Write (w / 2)

  • File: Can modify the file's contents, truncate it, or delete it
  • Directory: Can create, rename, or delete files within the directory

Execute (x / 1)

  • File: Can run the file as a program (a PHP script, shell script, binary)
  • Directory: Can cd into the directory and access its contents

Octal notation: Permissions are expressed as a 3-digit octal number (or 4-digit including the setuid/setgid/sticky bit):

  • 7 = rwx (4+2+1)
  • 6 = rw- (4+2)
  • 5 = r-x (4+1)
  • 4 = r-- (4)
  • 0 = --- (no permissions)

Common permission patterns:

  • 644 — owner read/write, group and others read-only (typical for web files)
  • 755 — owner full access, group and others read/execute (typical for directories and scripts)
  • 600 — owner read/write only (config files with secrets)
  • 777 — everyone full access (avoid — security risk)

In PHP, chmod('/path/to/file', 0644) sets these permissions (note the leading 0 for octal notation in PHP).

↑ Back to top

Follow-up 1

Can you explain how these permissions affect a user's interaction with a file?

When a user has 'read' permission for a file, they can open and view the contents of the file. With 'write' permission, they can modify the file, such as adding or deleting content. 'Execute' permission allows the user to run the file as a program or script. Without the necessary permissions, the user will receive an error or be denied access to perform the requested action.

Follow-up 2

What happens if a file has 'execute' permission but not 'read' permission?

If a file has 'execute' permission but not 'read' permission, the user will be able to execute the file as a program or script, but they will not be able to view the contents of the file. This means they can run the file, but they won't be able to see what the file contains.

Follow-up 3

How do these permissions apply to directories?

In PHP, 'read' permission for a directory allows a user to view the list of files and subdirectories within the directory. 'Write' permission allows a user to create, delete, or rename files and subdirectories within the directory. 'Execute' permission for a directory allows a user to access the contents of the directory, such as opening files or navigating into subdirectories. Without the necessary permissions, the user will be denied access to perform the requested action.

4. How do you set file permissions for different user groups in PHP?

In PHP, chmod() is used to set file or directory permissions. It takes a file path and a permissions value in octal notation.

chmod('/var/www/app/storage', 0755);   // directory: owner rwx, group rx, others rx
chmod('/var/www/app/config/db.php', 0640); // config file: owner rw, group r, others none
chmod('/var/www/app/uploads/file.txt', 0644); // file: owner rw, group r, others r

Octal permission breakdown: Each digit represents permissions for owner, group, and others respectively:

  • 4 = read, 2 = write, 1 = execute
  • 7 = 4+2+1 = read+write+execute
  • 6 = 4+2 = read+write
  • 5 = 4+1 = read+execute
  • 4 = read only
// Must use octal literal (leading 0) — NOT a string
chmod($file, 0644); // correct
chmod($file, '644'); // WRONG — treated as decimal 644, not octal
chmod($file, 644);   // WRONG — decimal 644 ≠ octal 0644

Return value: true on success, false on failure (check permissions of the running PHP process).

Verify the change:

chmod($file, 0644);
clearstatcache(); // clear PHP's stat cache before checking
echo substr(sprintf('%o', fileperms($file)), -4); // "0644"

PHP process limitation: chmod() only works if the PHP process (running as the web server user, e.g., www-data) owns the file or has sufficient privilege. On shared hosting, this is often restricted.

↑ Back to top

Follow-up 1

What are the different user groups?

In PHP, the different user groups are:

  • Owner: The user who owns the file or directory.
  • Group: The group that the owner belongs to.
  • Others: All other users who are not the owner or part of the group.

Follow-up 2

Can you provide a code example of setting file permissions for different user groups?

Sure! Here's an example of setting read and write permissions for the owner, and read-only permissions for the group and others:


Follow-up 3

What are the potential implications of setting incorrect permissions for different user groups?

Setting incorrect permissions for different user groups can have various implications:

  • Security vulnerabilities: If you set overly permissive permissions, unauthorized users may be able to access or modify sensitive files.
  • Data integrity issues: If you set restrictive permissions, legitimate users may not be able to access or modify necessary files.
  • System instability: Incorrect permissions can cause errors or conflicts within the system, leading to instability or malfunction.

It is important to carefully consider the permissions you set to ensure the security and functionality of your PHP applications.

5. What is the 'chmod' function in PHP and how is it used?

chmod() (change mode) is a PHP function that changes the permission mode of a file or directory. It mirrors the Unix chmod command.

Syntax:

chmod(string $filename, int $permissions): bool

Usage:

// Set a directory to be writable by the web server
chmod('/var/www/app/storage/logs', 0775);

// Protect a credentials file — only owner can read/write
chmod('/var/www/app/.env', 0600);

// Make a shell script executable
chmod('/var/scripts/deploy.sh', 0755);

Critical syntax rule — use octal literals:

chmod($file, 0644); // correct: octal literal (leading zero)
chmod($file, 644);  // WRONG: decimal 644 = octal 1204 — sets wrong permissions
chmod($file, '0644'); // WRONG: string, not integer

Return value:

  • true — permissions changed successfully
  • false — failed (likely because the PHP process doesn't own the file)

Checking after change:

chmod($file, 0644);
clearstatcache();  // clear PHP's cached stat info
echo decoct(fileperms($file) & 0777); // outputs "644"

Common use cases in PHP:

  • After moving an uploaded file, set appropriate permissions
  • During deployment scripts, ensure log and cache directories are writable
  • During installation wizards, check and fix directory permissions

Security reminder: Never set files to 0777 on a production server. Prefer the minimum permissions required.

↑ Back to top

Follow-up 1

Can you explain the syntax of the 'chmod' function?

The syntax of the 'chmod' function in PHP is as follows:

bool chmod ( string $filename , int $mode )

The 'filename' parameter specifies the path to the file or directory for which the permissions need to be changed. The 'mode' parameter specifies the new permissions to be set. It can be specified using either an octal number or a string representation.

Follow-up 2

What are the possible values that can be passed to the 'chmod' function?

The 'chmod' function in PHP accepts two types of values for the 'mode' parameter:

  1. Octal Number: The octal number represents the permissions using a three-digit format. Each digit represents the permissions for the owner, group, and others respectively. The possible values for each digit are:

    • 0: No permission
    • 1: Execute permission
    • 2: Write permission
    • 3: Write and execute permissions
    • 4: Read permission
    • 5: Read and execute permissions
    • 6: Read and write permissions
    • 7: Read, write, and execute permissions
  2. String Representation: The string representation allows specifying the permissions using a combination of letters. The possible letters are:

    • 'r': Read permission
    • 'w': Write permission
    • 'x': Execute permission
    • '-': No permission

For example, '755' and 'rwxr-xr-x' both represent the same set of permissions.

Follow-up 3

Can you provide a code example of using the 'chmod' function to change file permissions?

Certainly! Here's an example of using the 'chmod' function in PHP to change the permissions of a file:


In this example, the 'chmod' function is used to change the permissions of the 'file.txt' file to '0644'. The octal representation '0644' corresponds to read and write permissions for the owner, and read-only permissions for the group and others. The function returns a boolean value indicating whether the permissions were successfully changed or not.

Live mock interview

Mock interview: File Permissions

Intermediate ~5 min Your own free AI key

Your voice and your AI key never touch our servers; the key stays in this browser and is sent only to Google. Only your round scores are saved to track progress.