Re: Read directory; store filenames found in mySQL table? :: SOLVED!!
| From: | Rahul S. Johari | Date: | Wed, 13 Jan 2010 16:02:51 +0000 |
| Subject: | Re: Read directory; store filenames found in mySQL table? :: SOLVED!! | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-301068@lists.php.net to get a copy of this message | ||
Thanks All (Especially Warren); Final Code:
function dirList ($directory) {
$results = array();
$handler = opendir($directory);
while ($file = readdir($handler)) {
if ($file != '.' && $file != '..')
$results[] = $file;
}
closedir($handler);
return $results;
}
$origin = "pathto/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); } $db = mysql_connect("localhost","usr","pwd"); mysql_select_db("db",$db); foreach($fileListArr AS $value) { $sql="INSERT INTO r2 (ID, RECORDING) VALUES('','$value')"; $result = mysql_query($sql) or die (mysql_error()); } --- Rahul Sitaram Johari Founder, Internet Architects Group, Inc. [Email] sleepwalker@rahulsjohari.com [Web] http://www.rahulsjohari.com