I need to scan through a 30MB text file - it's a list of world cities - How can I access this file, I feel like a File_Get_Contents will give my server a stroke
Asked
Active
Viewed 4,148 times
3
-
1See http://stackoverflow.com/questions/4822165/php-read-large-text-file-log – Paul DelRe Apr 20 '11 at 21:52
-
30mb isn't huge. I've processed 4gb files with PHP before. It's possible... – ircmaxell Apr 20 '11 at 22:00
-
1Did you actually try `file_get_contents`? – Marcel Korpel Apr 20 '11 at 22:04
3 Answers
1
Filesystem functions come handy in this situation.
Example
$filename = "your_file_path";
// to open file
$fp = fopen($filename, 'r'); // use 'rw' to open file in read/write mode
// to output entire file
echo fread($fp, filesize($filename));
// to close file
fclose($fp);
References
(some handy functions)

Community
- 1
- 1

Wh1T3h4Ck5
- 8,399
- 9
- 59
- 79
0
<?php
$fh = @fopen("inputfile.txt", "r");
if ($fh) {
while (($line = fgets($fh)) !== false) {
echo $line;
// do something with $line..
}
fclose($fh);
}
?>
More information/examples on http://pt.php.net/manual/en/function.fgets.php

jsmp
- 162
- 1
-
2yet another case where the '@' operator is not only useless, but even harmfull ;) – Strae Apr 20 '11 at 22:09
-
worth mentioning why: the **@** operator will disable any error reporting even severe ones, file not found, permissions and so on, and could make hard the debug process. Also the complementary else could come in handy. – jsmp Apr 20 '11 at 22:32