PHP File Permissions & Security
File permissions and security are critical for protecting your application. Learn to manage permissions and validate file paths safely!
Output
Click Run to execute your code
Check Permissions
<?php
$file = 'example.txt';
echo is_readable($file) ? 'Readable' : 'Not readable';
echo is_writable($file) ? 'Writable' : 'Not writable';
echo is_executable($file) ? 'Executable' : 'Not executable';
?>
Change Permissions
<?php
// chmod(file, permissions)
chmod('file.txt', 0644); // rw-r--r--
chmod('script.sh', 0755); // rwxr-xr-x
?>
Sanitize Filename
<?php
function sanitizeFilename($filename) {
$filename = basename($filename);
$filename = preg_replace('/[^a-zA-Z0-9._-]/', '', $filename);
return $filename;
}
$safe = sanitizeFilename($_GET['file']);
?>
Summary
- chmod(): Change permissions
- is_readable(): Check read access
- basename(): Prevent directory traversal
- Validate paths: Always sanitize user input
What's Next?
Congratulations! You've completed Module 9. Next, dive into Database Integration with MySQL!
Enjoying these tutorials?