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

PHP Reading Files

एक file पढ़ना एक book खोलकर उसके pages से गुज़रने जैसा है। PHP file खोलता है, इसके contents line by line या एक साथ पढ़ता है, फिर इसे बंद कर देता है।
Syntax
php
$content = file_get_contents("filename.txt");   // whole file

$handle = fopen("filename.txt", "r");
$line = fgets($handle);   // one line
fclose($handle);

file_get_contents() से पढ़ना

fopen($path, r) एक file को read mode में खोलता है और एक resource handle return करता है जिसे आप दूसरे file functions को pass करते हैं, या अगर file exist नहीं करती या खोली नहीं जा सकती तो false, जिसे आपको हमेशा check करना चाहिए।

उदाहरण: Reading with file_get_contents()

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");
// Check whether `$handle`
if ($handle) {
    // Print "File opened for reading" to the output
    echo "File opened for reading";
    // Call `fclose($handle)`
    fclose($handle);
}
?>

fgets() से Line-by-Line पढ़ना

fread($handle, $length) किसी खुली file से दी गई bytes की संख्या तक पढ़ता है, जबकि fgets($handle) सिर्फ अगली line पढ़ता है, जो आमतौर पर line-oriented text files के लिए ज़्यादा natural choice है।

उदाहरण: Reading Line-by-Line with fgets()

php
<?php
// Call `file_put_contents("data.txt", "First line\nSecond line")`
file_put_contents("data.txt", "First line\nSecond line");
// Declare `$handle`, set to `fopen("data.txt", "r")`
$handle = fopen("data.txt", "r");
// Print `fread($handle, 5) . "\n"` to the output
echo fread($handle, 5) . "\n";
// Call `fclose($handle)`
fclose($handle);

// Declare `$handle`, set to `fopen("data.txt", "r")`
$handle = fopen("data.txt", "r");
// Print `fgets($handle)` to the output
echo fgets($handle);
// Call `fclose($handle)`
fclose($handle);
?>

fgetc() से Character-by-Character पढ़ना

file_get_contents($path) एक ही call में पूरी file को एक single string में पढ़ता है, जो तब manually एक handle खोलने, पढ़ने, और बंद करने से simpler है जब आपको बस एक साथ पूरी file के contents चाहिए हों।

उदाहरण: Reading Character-by-Character with fgetc()

php
<?php
// Call `file_put_contents("data.txt", "Hello World")`
file_put_contents("data.txt", "Hello World");
// Print `file_get_contents("data.txt")` to the output
echo file_get_contents("data.txt");
?>

file() से Files को Arrays की तरह पढ़ना

feof($handle) check करता है कि read position file के अंत तक पहुँच गई है या नहीं, और आमतौर पर एक while loop के अंदर fgets() से line by line file पढ़ते समय loop condition की तरह इस्तेमाल होता है।

उदाहरण: Reading Files as Arrays 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 `$handle`, set to `fopen("data.txt", "r")`
$handle = fopen("data.txt", "r");
// Keep looping while `!feof($handle)` holds
while (!feof($handle)) {
    // Print `fgets($handle)` to the output
    echo fgets($handle);
}
// Call `fclose($handle)`
fclose($handle);
?>

fread() से Custom Bytes पढ़ना

काम खत्म होते ही हमेशा एक file handle को fclose() से बंद करें, या file_get_contents() पर भरोसा करें जो यह automatically handle करता है, क्योंकि handles को खुला छोड़ना load के तहत server के available file descriptors खत्म कर सकता है।

उदाहरण: Reading Custom Bytes with fread()

php
<?php
// Call `file_put_contents("data.txt", "Some content here")`
file_put_contents("data.txt", "Some content here");
// Declare `$handle`, set to `fopen("data.txt", "r")`
$handle = fopen("data.txt", "r");
// Print `fread($handle, 4)` to the output
echo fread($handle, 4);
// 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. एक बड़ी file पर file_get_contents इस्तेमाल करना, जो इसे पूरी तरह memory में load कर देता है।
  2. पहले यह check न करना कि file exist करती है, जो एक warning raise करता है।
  3. यह भूल जाना कि fgets हर line में trailing newline रखता है।
🔒

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.