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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




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@xxxxxxxxxxxxxxxx
[Web]	http://www.rahulsjohari.com





--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux