I ran into an issue today with files uploaded through the WordPress Backend not getting the correct permissions. The permission on the new 2018 folder in the uploads directory as well as the files within it were too tight, the webserver user couldn’t even access them. I found the constants FS_CHMOD_DIR and FS_CHMOD_FILE that can be used to overwrite the default permissions for uploaded files. The WordPress Codex suggests using the the following settings in the wp-config.php:

define( 'FS_CHMOD_DIR', ( 0755 & ~ umask() ) );
define( 'FS_CHMOD_FILE', ( 0644 & ~ umask() ) );

However, I haven’t been able to find documentation on the syntax of those constants. The octal notation of the permissions is clear, but what is up with the ampersand, the swung dash and the umask command without a parameter? This article by wpbeginner mentions those constants as well, however it only mentions defining the constants with the octal values alone. What’s the difference between that and the longer command above?

1 Answer
1

The basic syntax for define() is:

define ( $name, $value )

In the above definition, the value is:

( 0755 & ~ umask() )

The ‘&’ (ampersand) is the ‘And’ bitwise operator,
the ‘~’ (tilde) is the ‘Not’ bitwise operator, and
the umask() function returns the current umask.

To answer your question in short (if your are interested in learning bitwise operations, there are plenty of resources on that), the above “syntax” is in fact an operation, that substracts the current umask from the definition you provide.

So, for example, if you provide 0644 and the current umask is 0640, then the effective permissions set will be 640. Why or when would you do that? Well, you would in the cases where you want to specify certain permissions, but not being more permissive than the default umask.

Leave a Reply

Your email address will not be published. Required fields are marked *