Solving “PHP is_writable Always Returns FALSE” Mysteries
Have you ever found yourself pulling your hair out when PHP’s is_writable function stubbornly returned FALSE, even though the folder clearly had the correct permissions? You’re not alone. Let me guide you through this common PHP quirk and help you get to the bottom of it.
The Persistent is_writable Problem
You’ve tried every trick in the bookâchanging permissions with chmod 777, even changing ownership to root:apache. Yet, when you run your script:
if (!is_writable($destinationFolder)) {
throw new Exception('Folder: '.$destinationFolder.' is not writable or does not exists.');
}
It keeps throwing that dreaded exception. The folder definitely exists, and the permissions look right in the file manager. So why the heck won’t PHP cooperate?
The Hidden Culprits
Before we dive into solutions, let’s understand what might be going on behind the scenes.
1. SELinux Contexts
If you’re running on a Linux system with SELinux enabled (common on CentOS/RHEL systems), it could be causing issues. Even if your folder has ‘write’ permissions, SELinux might restrict access.
To check this:
ls -Z /path/to/your/folder
Look for anything like httpd_sys_rw_content_t or httpd_sys_script_rw_content_t. If it’s there and you’re not expecting it, try temporarily disabling SELinux with:
sudo setenforce 0
2. Symlinks
If the folder in question is a symbolic link, PHP might be inspecting the target directory instead of the symlink itself.
Use realpath to resolve this:
$destinationFolder = realpath($destinationFolder);
if (!is_writable($destinationFolder)) {
// ...
}
3. Safe Mode
While it’s rare these days, safe mode could still be enabled in some legacy environments. If so, is_writable might not behave as expected.
Use this to check:
var_dump(ini_get('safe_mode'));
The Ultimate Solution: Try-Catch and Error Handling
Instead of relying solely on is_writable, which can sometimes return false positives, try to actually write a test file. If it works, the folder is indeed writable.
Here’s an example:
$destinationFolder = '/path/to/your/folder';
$testFile = $destinationFolder . '/test.txt';
if (!file_exists($destinationFolder)) {
throw new Exception('Directory does not exist.');
}
try {
if (touch($testFile) === FALSE) {
throw new Exception('Could not create test file: ' . $testFile);
}
} catch (\Exception $e) {
throw new Exception('Directory is not writable or has permission issues.', 0, $e);
}
unlink($testFile);
This code attempts to create a temporary test.txt file. If it succeeds, the folder is writable. If an exception occurs during the creation process, it’s caught and rethrown with more context.
Conclusion
The PHP is_writable function can sometimes be less than reliable. To ensure your scripts always work as expected, I recommend using a combination of permission checks and actual write attempts, as shown in the example above.
Remember to keep your directories clean, avoid symlinks when possible, and check for SELinux contexts if you’re on a Linux system. With these best practices in mind, those pesky is_writable issues should be a thing of the past.
For more detailed PHP development insights and tutorials, feel free to explore our blog at LaravelCompany. We cover everything from Laravel migrations to advanced PHP optimization techniques.