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

PHP File Create/Write

Files पढ़ना file handling का सिर्फ आधा हिस्सा है -- scripts को अक्सर नई files बनाने या मौजूदा में data लिखने की भी ज़रूरत होती है, जैसे एक log entry save करना, एक report generate करना, या uploaded content store करना। एक write mode में fopen(), fwrite() या simpler file_put_contents() के साथ pair किया गया, exactly यही handle करता है।
Syntax
php
file_put_contents("filename.txt", $data);   // create or overwrite

$handle = fopen("filename.txt", "a");        // "w" overwrites, "a" appends
fwrite($handle, $data);
fclose($handle);

file_put_contents() से एक File लिखना

file_put_contents($filename, $data) एक ही call में दी गई string को एक file में लिखता है, अगर file पहले से exist न करे तो इसे बनाते हुए -- manually एक file handle खोले, लिखे, और बंद किए बिना disk पर data save करने का सबसे simple तरीका।

उदाहरण: Writing a File with file_put_contents()

php
<?php
// Call `file_put_contents("data.txt", "Saved in one call")`
file_put_contents("data.txt", "Saved in one call");
// Print `file_get_contents("data.txt")` to the output
echo file_get_contents("data.txt");
?>

fopen() से Writing के लिए एक File खोलना

fopen($filename, "w") specifically writing के लिए एक file खोलता है -- अगर file पहले से exist करती है, इसके contents तुरंत erase हो जाते हैं; अगर यह exist नहीं करती, PHP उस path पर एक नई, खाली file बना देता है, fwrite() calls के लिए तैयार।

उदाहरण: Opening a File for Writing with fopen()

php
<?php
// Call `file_put_contents("data.txt", "old content")`
file_put_contents("data.txt", "old content");
// Declare `$handle`, set to `fopen("data.txt", "w")`
$handle = fopen("data.txt", "w");
// Call `fwrite($handle, "new content")`
fwrite($handle, "new content");
// Call `fclose($handle)`
fclose($handle);
// Print `file_get_contents("data.txt")` to the output
echo file_get_contents("data.txt");
?>

बिना Erase किए एक File में Append करना

fopen($filename, "a") एक file को append mode में खोलता है -- fwrite() से लिखा गया नया content file के मौजूदा contents के अंत में add होता है, उन्हें erase करने के बजाय, जो exactly वह है जो आप एक बढ़ती हुई log file जैसी किसी चीज़ के लिए चाहते हैं।

उदाहरण: Appending to a File Without Erasing It

php
<?php
// Call `file_put_contents("log.txt", "First entry\n")`
file_put_contents("log.txt", "First entry\n");
// Declare `$handle`, set to `fopen("log.txt", "a")`
$handle = fopen("log.txt", "a");
// Call `fwrite($handle, "Second entry\n")`
fwrite($handle, "Second entry\n");
// Call `fclose($handle)`
fclose($handle);
// Print `file_get_contents("log.txt")` to the output
echo file_get_contents("log.txt");
?>

सिर्फ तभी एक नई File बनाना जब यह पहले से Exist न करे

fopen() के लिए "x" mode writing के लिए एक नई file बनाता है, लेकिन fail हो जाता है (false return करते हुए) अगर उस path पर पहले से एक file exist करे -- तब उपयोगी जब एक मौजूदा file overwrite करना एक गलती होगी, जैसे एक uniquely-named export file generate करना।

उदाहरण: Creating a New File Only If It Doesn't Already Exist

php
<?php
// Declare `$handle`, set to `fopen("newfile.txt", "x")`
$handle = fopen("newfile.txt", "x");
// Print `$handle ? "Created" : "Already exists"` to the output
echo $handle ? "Created" : "Already exists";
if ($handle) fclose($handle);
// Declare `$handle2`, set to `@fopen("newfile.txt", "x")`
$handle2 = @fopen("newfile.txt", "x");
// Print `$handle2 ? "Created again" : " -- second attempt failed since it now exists"` to the output
echo $handle2 ? "Created again" : " -- second attempt failed since it now exists";
?>

Write Success Check करना और Errors Handle करना

fwrite() और file_put_contents() दोनों failure पर एक exception throw करने के बजाय false (या लिखे गए bytes की संख्या) return करते हैं -- उस return value को check करना एक failed write detect करने का इकलौता reliable तरीका है, जैसे एक full disk या missing directory permissions की वजह से हुआ।

उदाहरण: Checking Write Success and Handling Errors

php
<?php
// Declare `$result`, set to `file_put_contents("data.txt", "some data")`
$result = file_put_contents("data.txt", "some data");
// Check whether `$result === false`
if ($result === false) {
    // Print "Write failed" to the output
    echo "Write failed";
// Otherwise, run this branch
} else {
    // Print "$result bytes written" to the output
    echo "$result bytes written";
}
?>
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 में खोलना जब आप एक मौजूदा file के अंत में add करना चाहते थे -- "w" खुलते ही तुरंत file के मौजूदा contents को truncate (erase) कर देता है।
  2. fwrite() या file_put_contents() का return value check करना भूल जाना, दोनों failure पर (जैसे एक permissions problem) एक exception throw करने के बजाय false return करते हैं।
  3. target directory पर उचित server permissions set किए बिना किसी file में लिखना, हर write attempt को silently fail करवाते हुए।
चैप्टर सारांश
  • fopen($path, "w") writing के लिए एक file खोलता है, अगर यह exist न करे तो इसे बनाते हुए और अगर करे तो इसके मौजूदा contents erase करते हुए।
  • fopen($path, "a") appending के लिए एक file खोलता है, वहाँ पहले से मौजूद चीज़ को erase किए बिना अंत में नया content add करते हुए।
  • file_put_contents($path, $data) एक one-line shortcut है जो अलग से fopen/fwrite/fclose की ज़रूरत के बिना पूरी string को एक file में लिखता है।
🔒

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.