PHP Writing Files
In this page:
$handle = fopen("filename.txt", "w"); // "a" appends instead
fwrite($handle, $data);
fclose($handle);
file_put_contents("filename.txt", $data, FILE_APPEND);
fwrite() से लिखना
fopen($path, w) writing के लिए एक file खोलता है, और importantly पहले किसी भी मौजूदा content को truncate कर देता है — अगर आप किसी file को replace करने के बजाय उसमें add करना चाहते हैं, इसके बजाय a (append) mode इस्तेमाल करें।
उदाहरण: Writing with fwrite()
<?php
// Declare `$handle`, set to `fopen("data.txt", "w")`
$handle = fopen("data.txt", "w");
// Call `fwrite($handle, "First write")`
fwrite($handle, "First write");
// 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
fwrite() से Append करना
fwrite($handle, $data) एक खुली file handle को एक string लिखता है, actually लिखे गए bytes की संख्या return करते हुए, जिसे उन systems पर आपके data की length के against check करना उचित है जहाँ disk space खत्म हो सकता है।
उदाहरण: Appending with fwrite()
<?php
// Declare `$handle`, set to `fopen("data.txt", "a")`
$handle = fopen("data.txt", "a");
// Declare `$bytes`, set to `fwrite($handle, "Appended data")`
$bytes = fwrite($handle, "Appended data");
// Call `fclose($handle)`
fclose($handle);
// Print "$bytes bytes written" to the output
echo "$bytes bytes written";
?>
Login to try C/C++/Java/PHP code in the editor
file_put_contents() से Simple Writes
file_put_contents($path, $data) एक ही call में एक string को एक file में लिखता है, और किसी मौजूदा file को overwrite करने के बजाय उसके अंत में add करने के लिए एक FILE_APPEND flag accept करता है, fopen() के a mode जैसा।
उदाहरण: Simple Writes with file_put_contents()
<?php
// Call `file_put_contents("log.txt", "First entry\n")`
file_put_contents("log.txt", "First entry\n");
// Call `file_put_contents("log.txt", "Second entry\n", FILE_APPEND)`
file_put_contents("log.txt", "Second entry\n", FILE_APPEND);
// Print `file_get_contents("log.txt")` to the output
echo file_get_contents("log.txt");
?>
Login to try C/C++/Java/PHP code in the editor
file_put_contents() से Append करना
किसी ऐसी file में लिखना जिसे modify करने की permission web server process के पास नहीं है, आपकी error settings के आधार पर silently या एक warning के साथ fail हो जाता है, यही वजह है कि production code में fwrite() का return value check करना मायने रखता है।
उदाहरण: Appending with file_put_contents()
<?php
// Declare `$result`, set to `@file_put_contents("/root/protected.txt", "data")`
$result = @file_put_contents("/root/protected.txt", "data");
// Check whether `$result === false`
if ($result === false) {
// Print "Write failed -- check permissions" to the output
echo "Write failed -- check permissions";
}
?>
Login to try C/C++/Java/PHP code in the editor
Safe File Writing
configuration या exported results जैसे structured data के लिए, लिखने से पहले इसे json_encode() से JSON की तरह encode करना अक्सर बेहतर है, बजाय hand-formatting plain text के जिसे बाद में reliably वापस पढ़ना मुश्किल है।
उदाहरण: Safe File Writing
<?php
// Declare `$config` as an array: `["theme" => "dark", "lang" => "en"]`
$config = ["theme" => "dark", "lang" => "en"];
// Call `file_put_contents("config.json", json_encode($config))`
file_put_contents("config.json", json_encode($config));
// Print `file_get_contents("config.json")` to the output
echo file_get_contents("config.json");
?>
Login to try C/C++/Java/PHP code in the editor
- किसी मौजूदा file पर
wmode इस्तेमाल करना और उसका old content खो देना; append करने के लिएaइस्तेमाल करें। file_put_contentsकोFILE_APPENDpass करना भूल जाना, जो फिर file को overwrite कर देता है।- बिना write permission वाले folder में लिखना, ताकि write fail हो जाए।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: