PHP File Permissions
In this page:
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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
Permissions बदलना
chmod($path, 0755) PHP से किसी file या directory की permissions बदलता है, हालाँकि पहली जगह वह change करने के लिए web server के अपने user account को parent directory पर पर्याप्त permission चाहिए।
उदाहरण: Changing Permissions
<?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);
?>
Login to try C/C++/Java/PHP code in the editor
File Ownership बदलना
एक common और dangerous shortcut किसी upload directory को 'बस काम करने' के लिए permissions को 777 (सबके लिए full access) set करना है — यह एक serious security risk है क्योंकि यह किसी shared server पर किसी भी user को उन files को modify करने देता है।
उदाहरण: Changing File Ownership
<?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";
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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"));
?>
Login to try C/C++/Java/PHP code in the editor
Safe Permissions Handling
File ownership और permissions अक्सर किसी server migration या deployment के बाद 'मेरी upload script अचानक काम करना बंद कर दी' bugs के असली कारण होते हैं, क्योंकि नए environment के user accounts पुराने से अलग हो सकते हैं।
उदाहरण: Safe Permissions Handling
<?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";
}
?>
Login to try C/C++/Java/PHP code in the editor
chmodको octal0755के बजाय755जैसे decimal number के साथ इस्तेमाल करना।- permissions को बहुत open set करना, जैसे
0777, जो एक security risk है। - यह भूल जाना कि
chmodfail हो जाता है अगर PHP user file का owner न हो।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: