PHP
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions
-
Section 4: Object-Oriented PHP
-
Section 5: Working with Data
-
Section 6: Modern PHP (PHP 8)
-
Section 7: Working with Files and Networking
-
Section 8: Common PHP Frameworks Overview
-
Section 9: Tooling and Ecosystem
-
Section 10: Practical Projects
-
Section 11: Interview Practice
-
Section 12: More Standard Library
-
Section 13: Data Structures and Algorithms in PHP
-
Section 14: More Practice Exercises
-
Section 15: More Security and Best Practices
-
Section 16: More Testing and Tooling
-
Section 17: WordPress-Style CMS Concepts
-
Section 18: Advanced OOP Practice
-
Section 19: More Web Fundamentals
-
Section 20: Database Practice
-
Section 21: PHP Manual: Array Functions
-
Section 22: PHP Manual: Date and Calendar Functions
-
Section 23: PHP Manual: Filesystem and Directory Functions
-
Section 24: PHP Manual: Filter and Var Handling
-
Section 25: PHP Manual: Math Functions
-
Section 26: PHP Manual: JSON and XML
-
Section 27: PHP Manual: Network and Stream Functions
-
Section 28: PHP Manual: Error and Exception Handling
-
Section 29: PHP Manual: Output Control and Misc
-
Section 30: PHP Manual: FTP, Zip, and Mail
-
Section 31: Modern PHP Frameworks Deep Dive
-
Section 32: PHP Design Patterns
-
Section 33: More Practice Exercises
-
Section 34: PHP Performance and Deployment
-
Section 35: More Interview Practice
-
Section 36: More PHP Standard Library
-
Section 37: PHP Concurrency and Async
-
Section 38: More Web Development Practice
-
Section 39: PHP Testing Deep Dive
-
Section 40: Composer and Package Development
-
Section 41: PHP Security Deep Dive
-
Section 42: More Practical Projects
-
Section 43: Legacy PHP Maintenance
-
Section 44: More Algorithm Practice
-
Section 45: Final Practice and Review
-
Section 46: PHP for E-Commerce Patterns
-
Section 47: PHP API Design Deep Dive
-
Section 48: PHP Caching Strategies
-
Section 49: PHP Queue and Background Jobs
-
Section 50: PHP Multi-Tenancy Patterns
-
Section 51: PHP Real-Time Features
-
Section 52: PHP CMS and Content Modeling
-
Section 53: PHP Internationalization
-
Section 54: More Framework-Specific Practice
-
Section 55: PHP Legacy Code Refactoring
-
Section 56: More Practice Projects Round 2
-
Section 57: PHP Command-Line Applications
-
Section 58: PHP and Microservices
-
Section 59: More Interview and Review Round 2
100: File Permission Functions
Imagine you're managing a high-security office building. You wouldn't just give every employee a master key that opens every door, right? Some people only need to enter the lobby (read-only), some need to be able to file paperwork in the archives (write access), and a few specialized technicians need to be able to actually flip the switches in the server room (execute access). If a janitor accidentally walks into the server room and flips a switch, your whole company goes offline.
Your server's file system works exactly like that office building. PHP doesn't just "interact" with files; it does so under the identity of a specific user (usually www-data or apache). If that user doesn't have the right "keycard" for a file, PHP will throw a permission denied error, and your app crashes. That's where permission functions come in.
Checking the Badge Before You Try the Door
I've seen too many developers just try to open a file and then write a giant try-catch block or an if statement to handle the failure. It's a bit clumsy. Instead, you should check if you even have the right to touch the file first. This is where is_readable() and is_writable() come in.
Let's say you have a settings.json file. You want to make sure your script can read it before you attempt to parse it into an array. If you don't check first, and the file is locked down, your script might trigger a warning that looks messy to the end user.
$configFile = 'settings.json';
if (is_readable($configFile)) {
$data = file_get_contents($configFile);
echo "Settings loaded successfully.";
} else {
// Now we know exactly why it failed: permissions, not a missing file
die("Error: The system cannot read the settings file. Check your server permissions.");
}
Changing the Locks with chmod()
Sometimes, your script needs to actually change the permissions of a file—maybe you're creating a temporary upload folder and you need to make sure the web server can actually write files into it. For this, we use chmod().
Here is where things get weird: octal notation. You'll see numbers like 0755 or 0644. That leading zero is non-negotiable in PHP; it tells PHP the number is in octal (base-8). If you forget the zero, you're passing a decimal integer, and you'll end up setting permissions that make absolutely no sense, potentially locking yourself out of your own files.
Think of 0755 as "I can do everything, but everyone else can only read and enter the folder." Here is how I usually handle a dynamic logs directory:
$logDir = 'logs/app_logs';
if (!is_dir($logDir)) {
mkdir($logDir, 0777, true); // Create it with full access initially
}
// Now, let's tighten it up.
// 0755: Owner can read/write/execute, group/others can read/execute.
if (!chmod($logDir, 0755)) {
error_log("Failed to set permissions on the log directory.");
}
The Execution Trap
You'll also see is_executable(). In the world of PHP, you rarely use this for .php files because those are interpreted by the PHP engine. You'll mostly use this if your PHP script is acting as a wrapper to run a system tool, like ffmpeg for video processing or git for version control. If is_executable('/usr/bin/ffmpeg') returns false, there is no point in trying to run the command; you already know the OS will block it.
📋 Practical Task
The Config File Permission Guardian
You are tasked with creating a "Health Check" script for a production environment. Your goal is to ensure that a critical configuration file named config.php is secure.
Write a PHP script that performs the following logic:
- Check if
config.phpexists. - If it exists, check if it is writable.
- If the file is writable, it's a security risk! Use
chmod()to change the permissions to0444(read-only for everyone). - If the file is already read-only, print a message saying "Configuration is secure."
- If the file does not exist, print "Critical Error: Configuration file missing."
Note: To test this locally, you will first need to create a dummy config.php file in the same directory as your script.
There are no comments for now.