The Myth That Safe Mode Made Ownership Irrelevant
It is a common misconception that disabling safe_mode in PHP removes all ownership checks for file system operations like chmod(). While safe mode did impose strict restrictionsâlimiting file access to those owned by the user running the scriptâit never negated the fundamental rules of Unix-like operating systems. When you see the warning chmod(): Operation not permitted, it is almost always a result of standard POSIX permissions, not a lingering PHP configuration issue.
In your specific scenario, the error stems from a mismatch between the user executing the script and the owner of the target file. You are running a local development stack (XAMPP) where the Apache HTTP Server runs under the daemon user, as confirmed by your ps aux | grep httpd output. Your PHP script executes in this context. However, the file file_permissions.php was originally owned by joelhernandez. Even with safe_mode off and open_basedir restrictions disabled, PHP cannot change the permissions of a file it does not own.
The operating system enforces a hard rule: a process can only modify the mode bits of files it owns (or has explicit write access to via group/other permissions in some contexts, but changing ownership is strictly for the owner). When you changed the ownership from joelhernandez to daemon, the operation succeeded because the executing user and the file owner now matched.
The Mechanics of chmod() Without Safe Mode
Disabling safe mode expands PHP's ability to interact with the file system, but it does not grant superuser privileges. It effectively removes the check that previously blocked changing permissions on files owned by other users. However, the underlying filesystem logic remains intact. If you attempt to run:
$filename = "file_permissions.php";
$currentOwner = posix_getpwuid(fileowner($filename))['name'];
$execUser = posix_getpwuid(posix_geteuid())['name'];
if ($currentOwner !== $execUser) {
echo "Cannot modify permissions: File is owned by {$currentOwner}, script runs as {$execUser}.";
} else {
chmod($filename, 0777);
}
This logic highlights that chmod() is restricted to the owner of the file. If the script is invoked via a web request or cron job, it inherits the user context of that process (e.g., daemon, _www, or nobody). You cannot simply assume that because safe mode is off, you can alter any file on the disk.
Development Environments and XAMPP
Working with XAMPP often leads to confusion regarding user contexts. Unlike production environments where Apache might run as a dedicated service account, local setups frequently default to generic users or inherit privileges from the terminal session in ways that differ between platforms. In your case, the web server is explicitly running as daemon. If you are on macOS, this is a standard behavior for XAMPP installations.
The solution is not to fight the operating system's permission model but to align your development workflow with it. You have two primary options:
Align Ownership: Change the ownership of your project directory to match the web server user.
sudo chown -R daemon:staff /path/to/your/projectThis is what you discovered worked in your test case. It allows the
daemonuser to executechmod()without issues.Align Execution Context: Run your PHP CLI scripts directly as the same user that owns the files. If you are testing permission changes via command line, ensure you are not running them as a different user than the web server expects in production.
Security Implications of Permission Manipulation
While chmod() works for the file owner, attempting to set permissions like 0777 on every file is rarely advisable. It grants read, write, and execute access to all users on the system, including unprivileged accounts. If a writable file in your application contains sensitive data or logic that could be exploited, allowing global write access introduces significant security risks.
In production environments, you should strictly limit the permissions of uploaded files or dynamic content. For instance, if your application generates user-uploaded images, the web server might need to write them initially, but once saved, the permissions should be tightened to prevent arbitrary script execution. This often involves a background process or a specific hook that adjusts permissions after the initial save, rather than leaving everything open indefinitely.
Conclusion
The error you encountered was never related to safe_mode deprecation or its absence. It was a straightforward ownership conflict resolved by changing the file's owner to match the web server user. Disabling safe mode simply lifted the artificial PHP-level barrier that previously prevented this, revealing the underlying operating system restrictions that were always there. To move forward without needing constant sudo access for every permission change, ensuring your development directory is owned by the daemon user is the most robust approach for XAMPP setups.