473,385 Members | 1,732 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Parsing file in an array with size limit

I am developing an application by which to parse the content from the
access_log and insert it into the database. Since each row is an
different entry, I am using file() to get the contents into an array
and manipulate each row by foreach(...) and insert/update in the
database accordingly.

If the file is small, it works well. If the file is large (say > 5mb),
it generates memory allocation error. However I came to know that the
allowed memory size can be increased, but I want a different approach
for it.

I want like read only first 100 rows (1 to 1000) in first loop and then
1001 to 2000 and so on. I want the the return value in an array.

With file_get_contents(), it is

ac_arr =

string(2575795) "69.254.218.11 - - [27/Apr/2006:10:30:00 -0700] "GET
/img/livechat.gif HTTP/1.1" 200 2450 "http://www.addr.com/faq.htm"
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.2)
Gecko/20060308 Firefox/1.5.0.2" -
69.254.218.11 - - [27/Apr/2006:10:30:00 -0700] "GET /img/blank.gif
HTTP/1.1" 200 43 "http://www.addr.com/faq.htm" "Mozilla/5.0 (Windows;
U; Windows NT 5.1; en-US; rv:1.8.0.2) Gecko/20060308 Firefox/1.5.0.2" -
69.254.218.11 - - [27/Apr/2006:10:30:00 -0700] "GET /img/tollfree.gif
HTTP/1.1" 200 2332 "http://www.addr.com/faq.htm" "Mozilla/5.0 (Windows;
U; Windows NT 5.1; en-US; rv:1.8.0.2) Gecko/20060308 Firefox/1.5.0.2" -
69.254.218.11 - - [27/Apr/2006:10:30:00 -0700] "GET /img/vers.gif
HTTP/1.1" 200 958 "http://www.addr.com/faq.htm" "Mozilla/5.0 (Windows;
U; Windows NT 5.1; en-US; rv:1.8.0.2) Gecko/20060308 Firefox/1.5.0.2" -


I want like that with file(),

ac_arr =

array(12576) {
[0]=>
string(215) "69.254.218.11 - - [27/Apr/2006:10:30:00 -0700] "GET
/img/livechat.gif HTTP/1.1" 200 2450 "http://www.addr.com/faq.htm"
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.2)
Gecko/20060308 Firefox/1.5.0.2" -
"
[1]=>
string(210) "69.254.218.11 - - [27/Apr/2006:10:30:00 -0700] "GET
/img/blank.gif HTTP/1.1" 200 43 "http://www.addr.com/faq.htm"
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.2)
Gecko/20060308 Firefox/1.5.0.2" -
"
[2]=>
string(215) "69.254.218.11 - - [27/Apr/2006:10:30:00 -0700] "GET
/img/tollfree.gif HTTP/1.1" 200 2332 "http://www.addr.com/faq.htm"
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.2)
Gecko/20060308 Firefox/1.5.0.2" -
"
[3]=>
string(210) "69.254.218.11 - - [27/Apr/2006:10:30:00 -0700] "GET
/img/vers.gif HTTP/1.1" 200 958 "http://www.addr.com/faq.htm"
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.2)
Gecko/20060308 Firefox/1.5.0.2" -
"

Is there any way to do that (pagination)

Thanks

May 3 '06 #1
3 4204
On Wed, 03 May 2006 04:26:21 -0700, ye*********@gmail.com wrote:
I am developing an application by which to parse the content from the
access_log and insert it into the database. Since each row is an different
entry, I am using file() to get the contents into an array and manipulate
each row by foreach(...) and insert/update in the database accordingly.

If the file is small, it works well. If the file is large (say > 5mb), it
generates memory allocation error. However I came to know that the allowed
memory size can be increased, but I want a different approach for it.


Why not use fgets, that returns the data a line at a time? So instead of
using your foreach you use:

$fh = fopen("access_log", "r");
while (!feof($fh)) {
$string = fgets($fh);
...
}
fclose($fh);

With regards to pagination, do that when it's in MySQL using LIMIT.

Cheers,
Andy
--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
http://www.gphpedit.org | PHP editor for Gnome 2
http://www.andyjeffries.co.uk | Personal site and photos

May 3 '06 #2
Thanks.

It worked.

May 3 '06 #3
On Wed, 03 May 2006 04:34:25 -0700, ye*********@gmail.com wrote:
Thanks.

It worked.


Cool, glad to be of help.

Cheers,
Andy

--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
http://www.gphpedit.org | PHP editor for Gnome 2
http://www.andyjeffries.co.uk | Personal site and photos

May 3 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Dave Smithz | last post by:
Hi There, I have a PHP script that sends an email with attachment and works great when provided the path to the file to send. However this file needs to be on the same server as the script. ...
9
by: Mantorok Redgormor | last post by:
If I am parsing a config file that uses '#' for comments and the config file itself is 1640 bytes, and the format is VARIABLE=VALUE, is it recommended to use a) fgetc (parse a character at a...
17
by: Joe Laughlin | last post by:
I've not used C much before, so I don't know how robust or good this code is. I'd appreciate any feedback or criticisms anyone has! Thanks, Joe #include <stdio.h> #include <string.h>
4
by: Jens Mittag | last post by:
Hi! In my code, I have an array of a structure, which I want to save to a binary file. When the array is just created, everything works fine, but when I change contents of the array, saving...
11
by: MrNobody | last post by:
I need to zip up an 8Gb file using C# - do you know of any freeware libraries I can use to accomplish this? I guess it needs to be Zip64 to support such file sizes. I tried something called...
1
by: shyaminf | last post by:
hi everybody! iam facing a problem with the transfer of file using servlet programming. i have a code for uploading a file. but i'm unable to execute it using tomcat5.5 server. kindly help me how to...
13
by: pereges | last post by:
Hi, can some one please tell me why this program is not able to function properly. I have a array a and i am trying to create a pointer array b which points to elements less than 40 in a. ...
2
by: Eric Sosman | last post by:
Eric Sosman wrote: Never mind; I see now what I overlooked on first reading: Still, it might be better to return a pointer to a dynamically- allocated region whose size is *known* to be...
3
by: anush | last post by:
I have a log file like this: 2008-07-24 17:20:33 W server: shutting down server 2008-07-24 17:20:33 W client: shutting down client I need to parse this using php. I have split them by...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.