PHP File Handling Introduction
In this page:
$handle = fopen("filename.txt", "mode"); // false on failure
fwrite($handle, $data);
fclose($handle);
fopen() से Files खोलना
PHP server की filesystem से files पढ़ और लिख सकता है, एक script को data persist करने, activity log करने, या हर task के लिए database की ज़रूरत के बिना downloadable content generate करने देते हुए।
उदाहरण: Opening Files with fopen()
<?php
// Declare `$handle`, set to `fopen("data.txt", "w")`
$handle = fopen("data.txt", "w");
// Check whether `$handle`
if ($handle) {
// Print "File opened successfully" to the output
echo "File opened successfully";
// Call `fclose($handle)`
fclose($handle);
}
?>
Login to try C/C++/Java/PHP code in the editor
fwrite() से Files लिखना
PHP में file operations core functions के एक छोटे set के आसपास घूमते हैं — fopen(), fread()/fwrite(), और fclose() — जो ज़्यादातर programming languages के file APIs में मौजूद open-use-close pattern को mirror करते हैं।
उदाहरण: Writing Files with fwrite()
<?php
// Declare `$handle`, set to `fopen("data.txt", "w")`
$handle = fopen("data.txt", "w");
// Call `fwrite($handle, "Hello, file!")`
fwrite($handle, "Hello, file!");
// Call `fclose($handle)`
fclose($handle);
// Print `file_get_contents("data.txt")` to the output
echo file_get_contents("data.txt");
?>
Login to try C/C++/Java/PHP code in the editor
fclose() से Files बंद करना
हमेशा check करें कि कोई file operation succeed हुआ या नहीं, क्योंकि fopen() default रूप से failure पर false (कोई exception नहीं) return करता है, और success मान लेने वाला code आगे confusing errors produce करेगा।
उदाहरण: Closing Files with fclose()
<?php
// Declare `$handle`, set to `fopen("data.txt", "w")`
$handle = fopen("data.txt", "w");
// Check whether `$handle === false`
if ($handle === false) {
// Print "Failed to open file" to the output
echo "Failed to open file";
// Otherwise, run this branch
} else {
// Call `fwrite($handle, "Saved data")`
fwrite($handle, "Saved data");
// Call `fclose($handle)`
fclose($handle);
// Print "File written and closed" to the output
echo "File written and closed";
}
?>
Login to try C/C++/Java/PHP code in the editor
File Existence Check करना
File permissions और paths real deployments में बहुत मायने रखते हैं: PHP के process को किसी दिए गए path को पढ़ने या लिखने के लिए OS-level permission चाहिए, और एक misconfigured permission production में सबसे common file-handling bugs में से एक है।
उदाहरण: Checking File Existence
<?php
// Call `file_put_contents("data.txt", "sample")`
file_put_contents("data.txt", "sample");
// Check whether `file_exists("data.txt") && is_writable("data.txt")`
if (file_exists("data.txt") && is_writable("data.txt")) {
// Print "File exists and is writable" to the output
echo "File exists and is writable";
}
?>
Login to try C/C++/Java/PHP code in the editor
unlink() से Files Delete करना
low-level functions से आगे, PHP file_get_contents() और file_put_contents() जैसे convenience wrappers offer करता है जो simple cases के लिए एक ही call में open/read-or-write/close sequence handle करते हैं।
उदाहरण: Deleting Files with unlink()
<?php
// Call `file_put_contents("temp.txt", "temporary data")`
file_put_contents("temp.txt", "temporary data");
// Print `file_get_contents("temp.txt") . "\n"` to the output
echo file_get_contents("temp.txt") . "\n";
// Call `unlink("temp.txt")`
unlink("temp.txt");
// Print `file_exists("temp.txt") ? "Still exists" : "Deleted successfully"` to the output
echo file_exists("temp.txt") ? "Still exists" : "Deleted successfully";
?>
Login to try C/C++/Java/PHP code in the editor
- handle इस्तेमाल करने से पहले
fopenके false return करने की जाँच न करना। fcloseभूल जाना, ताकि buffered data शायद न लिखा जाए।- गलत mode इस्तेमाल करना, जैसे
w, जो एक मौजूदा file erase कर देता है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: