Hi friend I want to read log files and want the log file data to populate in database. Is there any class or function available? so I can get help from there.
do you mean access logs ??? do you use cpanel, and what format are you keeping logs in, post a couple of lines from the top and the bottom of the log and I'll write something to turn it into an associative array, you have about 20 minutes untill I gotta go out, so b quick.
This one is upper portion It has more then 8 lakhs lines I want to retrieve all and udate it into database. This one is lower portion
function LogToArray( $log ) { if( file_exists( $log ) ) { foreach( file( $log ) as $number => $line ) { if( trim( $line ) ) { preg_match( '~([0-9\.]+) - - \[(.*?[^\]])\] "([A-Z]+) (.*?[^ ]) (.*?[^"])" ([0-9]+) ([0-9]+)~si', $line, $request ); if( $request[0] ) { $requests[ $number ] = array( 'REMOTE_ADDR' => $request[1], 'REQUEST_TIME' => $request[2], 'REQUEST_METHOD' => $request[3], 'REQUEST_URI' => $request[4], 'REQUEST_HTTP_VERSION' => $request[5], 'REQUEST_RESPONSE' => $request[6], 'REQUEST_LENGTH_SENT' => $request[7] ); } } } return $requests ; } } function SetupDataBase( $dblink ) { return mysql_query( 'CREATE TABLE `requests` ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , `REMOTE_ADDR` VARCHAR( 15 ) NOT NULL , `REQUEST_TIME` TEXT NOT NULL , `REQUEST_METHOD` VARCHAR( 10 ) NOT NULL , `REQUEST_URI` TEXT NOT NULL , `REQUEST_HTTP_VERSION` TEXT NOT NULL , `REQUEST_RESPONSE` TEXT NOT NULL , `REQUEST_LENGTH_SENT` INT NOT NULL ) ENGINE = MYISAM ;', $dblink ); } function LogToDb( $log, $dblink ) { foreach( LogToArray( $log ) as $num => $data ) { if( !mysql_query( sprintf( 'INSERT INTO `requests` VALUES("", "%s", "%s", "%s", "%s", "%s", "%s", "%s")', $data['REMOTE_ADDR'], $data['REQUEST_TIME'], $data['REQUEST_METHOD'], $data['REQUEST_URI'], $data['REQUEST_HTTP_VERSION'], $data['REQUEST_RESPONSE'], $data['REQUEST_LENGTH_SENT'] ), $dblink ) ) { return !die( sprintf( 'Cannot query database mysql said %s', mysql_error( $dblink ) ) ); } } print( 'Log parsed and inserted into database<br />' ); } function DbToScreen( $dblink ) { if( ! ( $result = mysql_query( 'SELECT * FROM `requests`' )) ) { return !die( sprintf( 'Cannot query database for requests' ) ); } else { print( '<h2>Log Table</h2>' ); print( '<table>' ); print( '<tr>' ); print( '<th>Remote Address</th>' ); print( '<th>Time</th>' ); print( '<th>Method</th>' ); print( '<th>URI</th>' ); print( '<th>HTTP Version</th>' ); print( '<th>Response</th>' ); print( '<th>Length Sent</th>' ); print( '</tr>' ); print( '<tr>' ); while( $assoc = mysql_fetch_assoc( $result ) ) { print( '<tr>' ); printf( '<td>%s</td>', $assoc['REMOTE_ADDR'] ); printf( '<td>%s</td>', $assoc['REQUEST_TIME'] ); printf( '<td>%s</td>', $assoc['REQUEST_METHOD'] ); printf( '<td>%s</td>', $assoc['REQUEST_URI'] ); printf( '<td>%s</td>', $assoc['REQUEST_HTTP_VERSION'] ); printf( '<td>%s</td>', $assoc['REQUEST_RESPONSE'] ); printf( '<td>%s</td>', $assoc['REQUEST_LENGTH_SENT'] ); print( '</tr>' ); } print( '</tr>' ); print( '</table>' ); } } PHP: First do $dblink = mysql_connect('localhost', 'root', ''); mysql_select_db( 'logs', $dblink ); SetupDataBase( $dblink ); PHP: then $dblink = mysql_connect('localhost', 'root', ''); mysql_select_db( 'logs', $dblink ); LogToDb( 'log.txt', $dblink ); PHP: then $dblink = mysql_connect('localhost', 'root', ''); mysql_select_db( 'logs', $dblink ); DbToScreen( $dblink ); PHP: have fun
function LogToArray( $log ) { if( file_exists( $log ) and ( $log = fopen( $log, 'r' ) ) ) { while( !feof( $log ) ) { if( preg_match( '~([0-9\.]+) - - \[(.*?[^\]])\] "([A-Z]+) (.*?[^ ]) (.*?[^"])" ([0-9]+) ([0-9]+)~si', fgets( $log, 4096 ), $request ) ) { $requests[ ] = array( 'REMOTE_ADDR' => $request[1], 'REQUEST_TIME' => $request[2], 'REQUEST_METHOD' => $request[3], 'REQUEST_URI' => $request[4], 'REQUEST_HTTP_VERSION' => $request[5], 'REQUEST_RESPONSE' => $request[6], 'REQUEST_LENGTH_SENT' => $request[7] ); } } fclose( $log ); return $requests ; } } PHP:
Sorry again the same error is giving while running the file. I am trying to retrieve more then 8000 hundred lines of data from a file
It really shouldnt matter if it only retrieves one line at a time, theres not much chance that one line can be over 8 megs