PHP File Open/Read
In this page:
$handle = fopen("filename.txt", "r");
$line = fgets($handle); // read one line
fclose($handle);
$content = file_get_contents("filename.txt");
fopen() से एक File खोलना
fopen($filename, $mode) दी गई file खोलता है और उस खुली file को represent करने वाला एक resource handle return करता है -- "r" mode इसे सिर्फ पढ़ने के लिए खोलता है, file की बिल्कुल शुरुआत में positioned, आगे आने वाले read functions के लिए ready।
उदाहरण: Opening a File with fopen()
<?php
// Call `file_put_contents("data.txt", "Sample content")`
file_put_contents("data.txt", "Sample content");
// Declare `$handle`, set to `fopen("data.txt", "r")`
$handle = fopen("data.txt", "r");
// Print `$handle ? "File opened" : "Failed"` to the output
echo $handle ? "File opened" : "Failed";
// Call `fclose($handle)`
fclose($handle);
?>
Login to try C/C++/Java/PHP code in the editor
file_get_contents() से पूरी File पढ़ना
file_get_contents($filename) एक ही call में पूरी file को एक single string में पढ़ता है, बिना fopen(), एक read loop, या fclose() की ज़रूरत के -- सबसे simple option जब आपको बस एक साथ पूरी file के contents चाहिए हों और file बहुत बड़ी न हो।
उदाहरण: Reading the Whole File with file_get_contents()
<?php
// Call `file_put_contents("data.txt", "Full file contents")`
file_put_contents("data.txt", "Full file contents");
// 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
fgets() से Line by Line पढ़ना
fgets($handle) किसी खुली file handle से एक single line पढ़ता है, हर बार call होने पर file pointer को अगली line की शुरुआत में move करते हुए -- fgets() को तब तक loop करना जब तक यह false return न करे, बिना पूरी file को एक साथ memory में load किए, एक बार में एक line process करने का standard तरीका है।
उदाहरण: Reading Line by Line with fgets()
<?php
// Call `file_put_contents("data.txt", "Line 1\nLine 2\nLine 3")`
file_put_contents("data.txt", "Line 1\nLine 2\nLine 3");
// Declare `$handle`, set to `fopen("data.txt", "r")`
$handle = fopen("data.txt", "r");
// Keep looping while `($line = fgets($handle)) !== false` holds
while (($line = fgets($handle)) !== false) {
// Print `$line` to the output
echo $line;
}
// Call `fclose($handle)`
fclose($handle);
?>
Login to try C/C++/Java/PHP code in the editor
file() से एक File को Array में पढ़ना
file($filename) पूरी file पढ़ता है और इसे एक array की तरह return करता है, हर element में एक line होती है -- file_get_contents() की simplicity को fgets() के line-by-line structure के साथ combine करते हुए, तब उपयोगी जब आप foreach इस्तेमाल करके lines पर loop चलाना चाहें।
उदाहरण: Reading a File into an Array with file()
<?php
// Call `file_put_contents("data.txt", "Line 1\nLine 2\nLine 3")`
file_put_contents("data.txt", "Line 1\nLine 2\nLine 3");
// Declare `$lines`, set to `file("data.txt")`
$lines = file("data.txt");
// Loop over `$lines`, binding each item to `$line`
foreach ($lines as $line) {
// Print `$line` to the output
echo $line;
}
?>
Login to try C/C++/Java/PHP code in the editor
fread() से एक Specific संख्या के Bytes पढ़ना
fread($handle, $length) किसी खुली file handle से $length bytes तक पढ़ता है, तब उपयोगी जब आपको precise control चाहिए कि एक बार में कितना data पढ़ा जाए -- अक्सर एक controlled call में पूरी binary या text file पढ़ने के लिए filesize() के साथ pair किया जाता है।
उदाहरण: Reading a Specific Number of Bytes with fread()
<?php
// Call `file_put_contents("data.txt", "Hello World")`
file_put_contents("data.txt", "Hello World");
// Declare `$handle`, set to `fopen("data.txt", "r")`
$handle = fopen("data.txt", "r");
// Declare `$size`, set to `filesize("data.txt")`
$size = filesize("data.txt");
// Print `fread($handle, $size)` to the output
echo fread($handle, $size);
// Call `fclose($handle)`
fclose($handle);
?>
Login to try C/C++/Java/PHP code in the editor
- इसके return किए handle से पढ़ने की कोशिश करने से पहले यह check करना भूल जाना कि fopen() actually succeed हुआ (यह failure पर false return करता है)।
- गलत mode में एक file खोलना -- जैसे "w" (write, जो file truncate करता है) जब आप इसे सिर्फ "r" से पढ़ना चाहते थे।
- काम खत्म होने पर fclose() call करना भूल जाना, file handle को ज़रूरत से ज़्यादा देर खुला छोड़ते हुए और long-running scripts पर server resources बर्बाद करते हुए।
- fopen($path, $mode) एक file खोलता है और एक resource handle return करता है, या अगर file नहीं खुल पाई तो false।
- fread(), fgets(), और file_get_contents() हर एक थोड़े अलग तरीके से file contents पढ़ते हैं, अलग-अलग situations के लिए suited।
- पढ़ना खत्म होते ही हमेशा fclose() से एक file handle बंद करें।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: