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

From: Date: Wed, 13 Jan 2010 15:40:50 +0000
Subject: Re: Read directory; store filenames found in mySQL table?
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-301066@lists.php.net to get a copy of this message
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? --- Rahul Sitaram Johari Founder, Internet Architects Group, Inc. [Email] sleepwalker@rahulsjohari.com [Web] http://www.rahulsjohari.com

« previous php.general (#301066) next »