How to parse .log file and insert into database in PHP?

re-writting a php script to work on windows.

  • I have this script for adding images in bulk to a "rate me" site, but it was written for a linux server and I have a windows (server 2003) server. this line of code is giving me errors: $filelist = explode("\n",`find $dir|sort`); I think this is because it is using the "find" command which is a linux command. I would like this portion (and any other part) to be re-written so it will work on my windows server. Thankyou! <?php // this is a quick and dirty bulk adder, but it does the trick.. // fill out the info below and pick a base username and how many pics you want // on each account. this script will generate the useraccounts and image records // You can log into the accounts later to edit descriptions and categories if you want // put this file in the same directory as imagevote. // not an official part of image vote - use carefully and delete after use (or duplicates will be added each time run) require ('config.php'); mysql_connect($host,$user,$pass); @mysql_select_db($database) or die( "Unable to select database"); $dir = "c:/rmp/rmp2/"; // name of a directory off your main image vote dir $dirurl = "http://www.website.org/rmp/rmp2/"; $category = "general"; // all pics will go to the same category. pick one here, must be a valid category $baseuser = "testuser"; // change this to your name $apassword = "looner"; // enter a password here for the created accounts to use $peruser = "40"; // how many pictures per user account created $aboutpic = "What do you think?"; $self = "5"; // add a first rating of this value $resize = "yes"; // do pics need resizing? (if wider than 335 or 400, depending on your layout) $status = "waiting"; // set images to this status (set to "waiting" if you want them to go to moderate panel) $filelist = explode("\n",`find $dir|sort`); // for each item (file) in the array... $num=1; $count2=1; $currentuser = $baseuser.$num; mysql_query("INSERT INTO $usertable (name, password, category, email, validate) VALUES('$currentuser','$apassword','$category','$admin','ok')") or die(mysql_error()); print "<br> creating user $currentuser"; for ($count=0;$count<count($filelist);$count++) { $count2++; if ($count2 > $peruser) {$count2=0; $num++; $currentuser = $baseuser.$num; mysql_query("INSERT INTO $usertable (name, password, category, email, validate) VALUES('$currentuser','$apassword','$category','$admin','ok')") or die(mysql_error()); print "<br> creating user $currentuser"; } $filename=$filelist[$count]; // get the filename (including preceding directory, ie: ./pics/mypic.gif) if (!is_dir($filename) && strlen($filename) > 4) { $filename=basename($filename); // strip to filename $url = $dirurl.$filename; mysql_query("INSERT INTO $imagetable (name, url, category, description, self, total, rate, average, resize, status, reason) VALUES('$currentuser','$url','$category','$aboutpic','$self','1','$self','$self','$resize','$status','new')") or die(mysql_error()); print "<br>inserting $filename into $currentuser account"; } } print "<br>complete!"; ?>

  • Answer:

    Hello, b3and1p: I have fixed the script to be OS independent and written in pure PHP, I have written a function that finds all the files and folders in an specified path, including subfolders and then sort and return the results in an array. I have tested the fix both in Linux and Windows, but I can't test the complete script because I don't have the tables you use, you can download the complete script from here: http://www.xpde.com/fixed.zip Here is the code, but I suggest you to download the file instead try copy and paste, because some lines will be truncated: <?php // this is a quick and dirty bulk adder, but it does the trick.. // fill out the info below and pick a base username and how many pics you want // on each account. this script will generate the useraccounts and image records // You can log into the accounts later to edit descriptions and categories if you want // put this file in the same directory as imagevote. // not an official part of image vote - use carefully and delete after use (or duplicates will be added each time run) require ('config.php'); mysql_connect($host,$user,$pass); //Added code function listdirectories($dir) { global $result; $result[]=$dir; $contents=array(); if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { if (($file!='.') && ($file!='..')) $contents[]=$dir."/".$file; } closedir($dh); } } reset($contents); while (list($k,$v)=each($contents)) { //echo $v."\n"; listdirectories($v); } } function findandsort($path) { global $result; $result=array(); listdirectories($path); sort($result); return($result); } //End of added code @mysql_select_db($database) or die( "Unable to select database"); $dir = "c:/rmp/rmp2/"; // name of a directory off your main image vote dir $dirurl = "http://www.website.org/rmp/rmp2/"; $category = "general"; // all pics will go to the same category. pick one here, must be a valid category $baseuser = "testuser"; // change this to your name $apassword = "looner"; // enter a password here for the created accounts to use $peruser = "40"; // how many pictures per user account created $aboutpic = "What do you think?"; $self = "5"; // add a first rating of this value $resize = "yes"; // do pics need resizing? (if wider than 335 or 400, depending on your layout) $status = "waiting"; // set images to this status (set to "waiting" if you want them to go to moderate panel) //$filelist = explode("\n",`find $dir|sort`); $filelist = findandsort($dir); // for each item (file) in the array... $num=1; $count2=1; $currentuser = $baseuser.$num; mysql_query("INSERT INTO $usertable (name, password, category, email, validate) VALUES('$currentuser','$apassword','$category','$admin','ok')") or die(mysql_error()); print "<br> creating user $currentuser"; for ($count=0;$count<count($filelist);$count++) { $count2++; if ($count2 > $peruser) { $count2=0; $num++; $currentuser = $baseuser.$num; mysql_query("INSERT INTO $usertable (name, password, category, email, validate) VALUES('$currentuser','$apassword','$category','$admin','ok')") or die(mysql_error()); print "<br> creating user $currentuser"; } $filename=$filelist[$count]; // get the filename (including preceding directory, ie: ./pics/mypic.gif) if (!is_dir($filename) && strlen($filename) > 4) { $filename=basename($filename); // strip to filename $url = $dirurl.$filename; mysql_query("INSERT INTO $imagetable (name, url, category, description, self, total, rate, average, resize, status, reason) VALUES('$currentuser','$url','$category','$aboutpic','$self','1','$self','$self','$resize','$status','new')") or die(mysql_error()); print "<br>inserting $filename into $currentuser account"; } } print "<br>complete!"; ?> Please, test the solution on your system and tell me if you have any problem, and don't hesitate to request for any clarification, we are here to help you. Regards.

b3and1p-ga at Google Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.