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

PHP File Open/Read

PHP server पर किसी file से data पढ़ने से पहले, इसे पहले उस file को खोलने और इसका एक handle पाने का एक तरीका चाहिए -- एक reference जिसे PHP हर आगे के read operation के लिए इस्तेमाल करता है। fopen() file खोलता है और वह handle return करता है, और related functions का एक family (fread, fgets, fgetcsv, और और) इसके contents को अलग-अलग तरीकों से पढ़ता है।
Syntax
php
$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
<?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);
?>

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

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

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

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
<?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);
?>
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. इसके return किए handle से पढ़ने की कोशिश करने से पहले यह check करना भूल जाना कि fopen() actually succeed हुआ (यह failure पर false return करता है)।
  2. गलत mode में एक file खोलना -- जैसे "w" (write, जो file truncate करता है) जब आप इसे सिर्फ "r" से पढ़ना चाहते थे।
  3. काम खत्म होने पर 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:

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.