PHP File Create/Write
In this page:
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
// 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");
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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");
?>
Login to try C/C++/Java/PHP code in the editor
बिना 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
// 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");
?>
Login to try C/C++/Java/PHP code in the editor
सिर्फ तभी एक नई 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
// 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";
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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";
}
?>
Login to try C/C++/Java/PHP code in the editor
- एक file को "w" mode में खोलना जब आप एक मौजूदा file के अंत में add करना चाहते थे -- "w" खुलते ही तुरंत file के मौजूदा contents को truncate (erase) कर देता है।
- fwrite() या file_put_contents() का return value check करना भूल जाना, दोनों failure पर (जैसे एक permissions problem) एक exception throw करने के बजाय false return करते हैं।
- 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: