3

I'd like to get input from txt file but the file contain data 1.6 GB. So it crashed when I load the data into my variable how can I get it in my php program

Tanapat Ruengsatra
  • 101
  • 1
  • 2
  • 7
  • 8
    So load a line from the file, process it, read the next line, process that, read the next, etc.... don't try to load it all in one go – Mark Baker Apr 01 '13 at 15:42
  • 2
    Why are you trying to process this file in PHP? Y...you're not actually trying to display 1.6GB of data in a web page, right? **Right?** –  Apr 01 '13 at 15:43
  • @MarkBaker - make your comment an answer. It's the right one. – Floris Apr 01 '13 at 15:44
  • 2
    he didn't say he's using it for web page. and those 1.6GB data could be in one line. – su- Apr 01 '13 at 15:46
  • @su- - No, he didn't. But I'm not sure why anyone would use PHP for anything other than web development, since that's what the language was built for. –  Apr 01 '13 at 15:48
  • 2
    people use php for scripting too – su- Apr 01 '13 at 15:48
  • 6
    @Jack - I do a lot of non-web stuff in PHP, and it's perfectly capable of doing that as well – Mark Baker Apr 01 '13 at 15:48
  • 1
    @JackManey That's the kind of assumption that make me comfortable closing this question. Not enough details. – Mike B Apr 01 '13 at 15:51
  • You can split the files into chunks if you are working with csv or log files http://stackoverflow.com/a/10271542/1226894 – Baba Apr 01 '13 at 16:00

2 Answers2

3

You can process it line per line :

$handle = fopen("file.txt", "r") or die("Couldn't get handle");
if ($handle) {
    while (!feof($handle)) {
        $buffer = fgets($handle, 4096);
        // Process here..
    }
    fclose($handle);
}
Oussama Jilal
  • 7,669
  • 2
  • 30
  • 53
1
$file = fopen($filename);
while ($line = fgets($file)) {
    // do stuff
}
fclose($file);
GabeIsman
  • 821
  • 7
  • 9