← Back to PHP Course | Chapter 9: File Handling | Lesson 5 of 8

PHP File Permissions

File permissions key cards जैसे हैं जो बताते हैं कि हर file को कौन खोल, बदल, या चला सकता है। वे आपके server को protect करते हैं ताकि strangers ऐसी चीज़ें न पढ़ या edit कर सकें जो उन्हें नहीं करनी चाहिए।
Syntax
php
is_readable($path);
is_writable($path);
chmod($path, 0644);
chown($path, $user);

Permissions Check करना

किसी Unix-like server पर हर file और directory में permission bits होते हैं जो control करते हैं कि कौन इसे पढ़, लिख, या execute कर सकता है, आमतौर पर 755 जैसे तीन digits में दिखाए जाते हैं, owner, group, और बाकी सबके लिए एक-एक।

उदाहरण: Checking Permissions

php
<?php
// Call `file_put_contents("data.txt", "content")`
file_put_contents("data.txt", "content");
// Print `substr(sprintf('%o', fileperms("data.txt")), -4)` to the output
echo substr(sprintf('%o', fileperms("data.txt")), -4);
?>

Permissions बदलना

chmod($path, 0755) PHP से किसी file या directory की permissions बदलता है, हालाँकि पहली जगह वह change करने के लिए web server के अपने user account को parent directory पर पर्याप्त permission चाहिए।

उदाहरण: Changing Permissions

php
<?php
// Call `file_put_contents("data.txt", "content")`
file_put_contents("data.txt", "content");
// Call `chmod("data.txt", 0755)`
chmod("data.txt", 0755);
// Print `substr(sprintf('%o', fileperms("data.txt")), -4)` to the output
echo substr(sprintf('%o', fileperms("data.txt")), -4);
?>

File Ownership बदलना

एक common और dangerous shortcut किसी upload directory को 'बस काम करने' के लिए permissions को 777 (सबके लिए full access) set करना है — यह एक serious security risk है क्योंकि यह किसी shared server पर किसी भी user को उन files को modify करने देता है।

उदाहरण: Changing File Ownership

php
<?php
// Setting 0777 gives everyone full access -- a common but risky shortcut
file_put_contents("data.txt", "content");
chmod("data.txt", 0644); // safer than 0777
echo "Permissions set to a safer default";
?>

Existence और Types Check करना

is_writable() और is_readable() आपको कोई file operation try करने से पहले PHP से permissions check करने देते हैं, ताकि आपकी script एक raw fwrite() warning के बजाय एक clear error के साथ gracefully fail हो सके।

उदाहरण: Checking Existence and Types

php
<?php
// Call `file_put_contents("data.txt", "content")`
file_put_contents("data.txt", "content");
// Print a detailed dump (with types) of `is_writable("data.txt")`
var_dump(is_writable("data.txt"));
// Print a detailed dump (with types) of `is_readable("data.txt")`
var_dump(is_readable("data.txt"));
?>

Safe Permissions Handling

File ownership और permissions अक्सर किसी server migration या deployment के बाद 'मेरी upload script अचानक काम करना बंद कर दी' bugs के असली कारण होते हैं, क्योंकि नए environment के user accounts पुराने से अलग हो सकते हैं।

उदाहरण: Safe Permissions Handling

php
<?php
// Call `file_put_contents("data.txt", "content")`
file_put_contents("data.txt", "content");
// Check whether `!is_writable("data.txt")`
if (!is_writable("data.txt")) {
    // Print "Cannot write -- check ownership after deployment" to the output
    echo "Cannot write -- check ownership after deployment";
// Otherwise, run this branch
} else {
    // Print "Write access confirmed" to the output
    echo "Write access confirmed";
}
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. chmod को octal 0755 के बजाय 755 जैसे decimal number के साथ इस्तेमाल करना।
  2. permissions को बहुत open set करना, जैसे 0777, जो एक security risk है।
  3. यह भूल जाना कि chmod fail हो जाता है अगर PHP user file का owner न हो।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.