Hello,
I am not very experienced with file handling, so I am
asking for a little help with this.
I have a file called "geotext_1a.csv "
and it is a csv file looking like this:
"2.6.190.56","2 .6.190.63","339 96344","3399635 1","GB","Uni ted Kingdom"
"3.0.0.0","4.17 .135.31","50331 648","68257567" ,"US","Unite d States"
"4.17.135.32"," 4.17.135.63","6 8257568","68257 599","CA","Cana da"
I want to take some data from it and write a file called geo_01.txt
and will look like this :
33996344,339963 51,United Kingdom
50331648,682575 67,United States
68257568,682575 99,Canada
Now I have written some code but I used
the file function to put the contents into an array.
As the file is a few megabytes big, this has caused memory
failure problems.
This is what I have:
So my question is
Does processing the file as a stream use
less memory ?
i.e. $handle = fopen("$file"," r")
Because I am not familiar with using this stuff
I don't know how to do this
Any guidance much appreciated
Many thanks
I am not very experienced with file handling, so I am
asking for a little help with this.
I have a file called "geotext_1a.csv "
and it is a csv file looking like this:
"2.6.190.56","2 .6.190.63","339 96344","3399635 1","GB","Uni ted Kingdom"
"3.0.0.0","4.17 .135.31","50331 648","68257567" ,"US","Unite d States"
"4.17.135.32"," 4.17.135.63","6 8257568","68257 599","CA","Cana da"
I want to take some data from it and write a file called geo_01.txt
and will look like this :
33996344,339963 51,United Kingdom
50331648,682575 67,United States
68257568,682575 99,Canada
Now I have written some code but I used
the file function to put the contents into an array.
As the file is a few megabytes big, this has caused memory
failure problems.
This is what I have:
Code:
<?php $file_array = file('geotext_1a.csv'); $data = array(); $i = 0; foreach ($file_array as $row) { $row = str_replace('"','',$row); // get rid of double quotes $cells = explode(",",$row); $data[$i]['trash1'] = $cells[0]; $data[$i]['trash2'] = $cells[1]; $data[$i]['ip1'] = $cells[2]; $data[$i]['ip2'] = $cells[3]; $data[$i]['trash3'] = $cells[4]; $data[$i]['country'] = $cells[5]; $i++; } echo '<pre>'; print_r($data); echo '</pre>'; ?>
Does processing the file as a stream use
less memory ?
i.e. $handle = fopen("$file"," r")
Because I am not familiar with using this stuff
I don't know how to do this
Any guidance much appreciated
Many thanks
Comment