Re: Read directory; store filenames found in mySQL table?

From: Date: Wed, 13 Jan 2010 21:21:50 +0000
Subject: Re: Read directory; store filenames found in mySQL table?
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-301083@lists.php.net to get a copy of this message
On Wed, Jan 13, 2010 at 10:50 AM, Rahul S. Johari <sleepwalker@rahulsjohari.com> wrote: > > On Jan 13, 2010, at 10:40 AM, Rahul S. Johari wrote: > >> >> On Jan 13, 2010, at 9:50 AM, Warren Windvogel wrote: >> >>> On 2010/01/13 04:25 PM, Rahul S. Johari wrote: >>>> >>>> Ave, >>>> >>>> This is what I'm trying to do; I want to read a directory (eg: W:\Test\) >>>> and take all the filenames found in the directory (eg: 1.vox, 2.wav, 3..txt) >>>> and store them in  a simple mySQL table. >>>> >>>> Can I do this? >>> >>> I tried to very quickly convert something I've done. It may need some >>> work. Will work in  linux env. >>> >>> $origin = "Path" >>> >>> #load file listing into an array >>> $shell = shell_exec("du $origin"); >>> $array  = array_reverse(explode("\n",$shell)); >>> $contIdArr = array(); >>> >>> $newArr = array(); >>> foreach($array as $elem){ >>>  $newDir = ""; >>>  $pathArray = explode("/", $elem); >>>  $nodeDepth = count($pathArray); >>>  for($count=1; $count<$nodeDepth; $count++){ >>>      $newDir = $newDir.$pathArray[$count].'/'; >>>  } >>>  $newArr[] = '/'.$newDir; >>> } >>> sort($newArr); >>> >>> >>> foreach($newArr as $dir){ >>>  $pathArray = explode("/", $dir); >>> >>>  $fileListArr = dirList($dir); >>> >>>  foreach($fileListArr as $file){ >>>      //Insert file($file) and current dir/path($dir) into db >>>  } >>> } >>> >>> Kind regards >>> Warren >>> >> >> >> Warren, >> >> I tried using your code and it definitely is very efficient & fast; >> however I'm running into a small problem and I'm not sure how to correct it. >> I'm getting the array with filenames from the folder I'm searching in PLUS >> all the root folders of the machine as well. >> >> This is the code I'm using (note that I'm just echoing the array right >> now; I'll move to inserting data into mySQL after): >> >> function dirList ($directory) { >>  $results = array(); >>  $handler = opendir($directory); >>  while ($file = readdir($handler)) { >>      if ($file != '.' && $file != '..') >>          $results[] = $file; >>  } >>  closedir($handler); >>  return $results; >> } >> >> $origin = "/Library/WebServer/Documents/folder1/folder2/images/"; >> >> #load file listing into an array >> $shell = shell_exec("du $origin"); >> $array  = array_reverse(explode("\n",$shell)); >> $contIdArr = array(); >> >> $newArr = array(); >> foreach($array as $elem){ >>  $newDir = ""; >>  $pathArray = explode("/", $elem); >>  $nodeDepth = count($pathArray); >>  for($count=1; $count<$nodeDepth; $count++){ >>      $newDir = $newDir.$pathArray[$count].'/'; >>  } >>  $newArr[] = '/'.$newDir; >> } >> sort($newArr); >> >> foreach($newArr as $dir){ >>  $pathArray = explode("/", $dir); >>  $fileListArr = dirList($dir); >> >>  foreach($fileListArr as $file){ >>        echo $file."<BR>"; >>      //Insert file($file) and current dir/path($dir) into db >>  } >> } >> >> >> As an output ... i get a list of all the files in the "images" folder >> preceeded by the all the list of root folders on my machine!! How do I >> eliminate the list of root folders? > > > Nevermind, I was looking at the wrong output. I got it!! I've got all my > filenames in my $fileListArr[] array!! > Now I just to get the values in a mySQL table. > > > --- > Rahul Sitaram Johari > Founder, Internet Architects Group, Inc. > > [Email] sleepwalker@rahulsjohari.com > [Web]   http://www.rahulsjohari.com > > > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > consider stacking the insert statements in sql to allow for a certain number of inserts in one connect. insert into my_table (field1, field2...fieldn) values('field1','field2'...fieldn),('field1','field2'...fieldn),('field1','field2'...fieldn),('field1','field2'...fieldn)... keep to something like 100 to avoid buffer overflows and it should make the inserts much faster -- Bastien Cat, the other other white meat

« previous php.general (#301083) next »