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

PHP Writing Files

एक file लिखना एक notebook में लिखने जैसा है। सावधान रहें, हालाँकि: इसे एक तरीके से खोलना पहले पुराने pages erase कर देता है, जबकि दूसरा तरीका अंत में नई lines add करता है।
Syntax
php
$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
<?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");
?>

fwrite() से Append करना

fwrite($handle, $data) एक खुली file handle को एक string लिखता है, actually लिखे गए bytes की संख्या return करते हुए, जिसे उन systems पर आपके data की length के against check करना उचित है जहाँ disk space खत्म हो सकता है।

उदाहरण: Appending with fwrite()

php
<?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";
?>

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
<?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");
?>

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
<?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";
}
?>

Safe File Writing

configuration या exported results जैसे structured data के लिए, लिखने से पहले इसे json_encode() से JSON की तरह encode करना अक्सर बेहतर है, बजाय hand-formatting plain text के जिसे बाद में reliably वापस पढ़ना मुश्किल है।

उदाहरण: Safe File Writing

php
<?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");
?>
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. किसी मौजूदा file पर w mode इस्तेमाल करना और उसका old content खो देना; append करने के लिए a इस्तेमाल करें।
  2. file_put_contents को FILE_APPEND pass करना भूल जाना, जो फिर file को overwrite कर देता है।
  3. बिना write permission वाले folder में लिखना, ताकि write fail हो जाए।
🔒

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.