REGEXVAULTv2.0
Dev & Systems/File Paths
Verified Safe

POSIX File Permission Octal Regex for PHP

/^([0-7]?)([0-7]{3})$/

What this pattern does

This page provides a lightweight, single-purpose regular expression for matching posix file permission octal, ported and verified for PHP. A rigorously tested regex reduces debugging time and protects your application from edge-case failures. The snippet below is ready to drop into your PHP project — whether you're validating in a Laravel validator, a WordPress plugin, or a standalone PHP script.

Php Implementation

Php
<?php
// POSIX File Permission Octal
// ReDoS-safe | RegexVault — Dev & Systems > File Paths

define('POSIX_FILE_PERMISSION_OCTAL_PATTERN', '/^([0-7]?)([0-7]{3})$/');

function validate_posix_file_permission_octal(string $input): bool {
    return (bool) preg_match(POSIX_FILE_PERMISSION_OCTAL_PATTERN, $input);
}

// Example
var_dump(validate_posix_file_permission_octal("755")); // bool(true)

Test Cases

Matches (Valid)
Rejects (Invalid)
755888
64499
77776
0008888
0755a644
47550855
2755
1777

When to use this pattern

This pattern is drawn from the Dev & Systems > File Paths category and carries a ReDoS-safe certification. That matters for PHP developers because especially relevant in PHP where PCRE backtracking limits can trigger silent failures on malicious input. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.

Common Pitfalls

Setting 777 on sensitive files is a security risk. The sticky bit on directories (1777) prevents users from deleting each other's files even with directory write permission.

Technical Notes

Optional leading digit: 4=setuid, 2=setgid, 1=sticky. Three digits: owner/group/other (read=4, write=2, execute=1). 1777 = /tmp permissions (sticky). 4755 = setuid root (dangerous). Group 1 = special bits, group 2 = rwx permissions.

Have a pattern that belongs in the vault?

Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.

Submit a Pattern