Google Answers Logo
View Question
 
Q: re-writting a php script to work on windows. ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: re-writting a php script to work on windows.
Category: Computers > Programming
Asked by: b3and1p-ga
List Price: $25.00
Posted: 07 Sep 2003 23:08 PDT
Expires: 07 Oct 2003 23:08 PDT
Question ID: 253370
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  
Subject: Re: re-writting a php script to work on windows.
Answered By: joseleon-ga on 07 Sep 2003 23:56 PDT
Rated:5 out of 5 stars
 
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 rated this answer:5 out of 5 stars and gave an additional tip of: $5.00
awesome! very quick response, Worked perfectly! Thanks for the help.

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy