<p>How can I check <code>file permissions</code>, without having to run operating system specific command via <code>passthru()</code> or <code>exec()</code>?</p>

[{'answer_id': 19469, 'author': 'Chris Hanson', 'author_id': 714, 'author_profile': 'https://Stackoverflow.com/users/714', 'pm_score': 0, 'selected': False, 'text': '<p>What do you want to do by checking file permissions?</p>\n\n<p>When writing secure code, it\'s almost always incorrect to "check, then do" anything.  The reason is that between the checking whether you can do something and actually doing it, the state of the system could change such that doing it would have a different result.</p>\n\n<p>For example, if you check whether a file exists before writing one, don\'t check whether you wrote the file successfully (or don\'t check in a detailed-enough fashion), and then later depend on the contents of the file you wrote, you could actually be reading a file written by an attacker.</p>\n\n<p>So instead of checking file permissions, just do whatever it was you were going to do if the permissions check succeeded, and handle errors gracefully.</p>\n'}, {'answer_id': 19470, 'author': 'Huppie', 'author_id': 1830, 'author_profile': 'https://Stackoverflow.com/users/1830', 'pm_score': 4, 'selected': False, 'text': '<p>You can use the <a href="http://www.php.net/manual/en/function.is-readable.php" rel="noreferrer">is_readable()</a>, <a href="http://www.php.net/manual/en/function.is-executable.php" rel="noreferrer">is_executable()</a> etc.. commands.</p>\n'}, {'answer_id': 19472, 'author': 'Željko Živković', 'author_id': 1926, 'author_profile': 'https://Stackoverflow.com/users/1926', 'pm_score': 6, 'selected': True, 'text': '<p>Use <a href="http://php.net/fileperms" rel="noreferrer">fileperms()</a> function</p>\n\n<pre><code>clearstatcache();\necho substr(sprintf(\'%o\', fileperms(\'/etc/passwd\')), -4);\n</code></pre>\n'}, {'answer_id': 11434314, 'author': 'OzzyCzech', 'author_id': 355316, 'author_profile': 'https://Stackoverflow.com/users/355316', 'pm_score': 1, 'selected': False, 'text': '<p>Use <a href="http://php.net/manual/en/function.fileperms.php" rel="nofollow">fileperms()</a> function and substring:</p>\n\n<pre><code>substr(decoct(fileperms(__DIR__)), -4); // 0777\nsubstr(decoct(fileperms(__DIR__)), -3); // 777\n</code></pre>\n\n<p>For file:</p>\n\n<pre><code>substr(decoct(fileperms(__FILE__)), -4); // 0644\nsubstr(decoct(fileperms(__FILE__)), -3); // 644\n</code></pre>\n\n<p>Replace <code>__FILE__</code> and <code>__DIR__</code> with your path or variable</p>\n'}, {'answer_id': 16083949, 'author': 'Maciej Sz', 'author_id': 1697320, 'author_profile': 'https://Stackoverflow.com/users/1697320', 'pm_score': 3, 'selected': False, 'text': '<p>Real coders use bitwise operations, not strings ;) This is much more elegant way of handling permissions:</p>\n\n<pre><code>function checkPerms($path)\n{\n    clearstatcache(null, $path);\n    return decoct( fileperms($path) &amp; 0777 );\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/19466', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/115/']