PHP Reading Files
In this page:
$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
// 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);
}
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
fgetc() से Character-by-Character पढ़ना
file_get_contents($path) एक ही call में पूरी file को एक single string में पढ़ता है, जो तब manually एक handle खोलने, पढ़ने, और बंद करने से simpler है जब आपको बस एक साथ पूरी file के contents चाहिए हों।
उदाहरण: Reading Character-by-Character with fgetc()
<?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");
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
fread() से Custom Bytes पढ़ना
काम खत्म होते ही हमेशा एक file handle को fclose() से बंद करें, या file_get_contents() पर भरोसा करें जो यह automatically handle करता है, क्योंकि handles को खुला छोड़ना load के तहत server के available file descriptors खत्म कर सकता है।
उदाहरण: Reading Custom Bytes with fread()
<?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);
?>
Login to try C/C++/Java/PHP code in the editor
- एक बड़ी file पर
file_get_contentsइस्तेमाल करना, जो इसे पूरी तरह memory में load कर देता है। - पहले यह check न करना कि file exist करती है, जो एक warning raise करता है।
- यह भूल जाना कि
fgetsहर line में trailing newline रखता है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: