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

From: Date: Wed, 13 Jan 2010 15:19:33 +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-301063@lists.php.net to get a copy of this message
On Jan 13, 2010, at 9:56 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.
Sorry. Forgot to include this. function dirList ($directory) { // create an array to hold directory list $results = array(); // create a handler for the directory $handler = opendir($directory); // keep going until all files in directory have been read while ($file = readdir($handler)) {
       // if $file isn't this directory or its parent,
       // add it to the results array
       if ($file != '.' && $file != '..')
           $results[] = $file;
} // tidy up: close the handler closedir($handler); // done! return $results; } If you're dealing with 1 directory you can use it to read all files in it to an array. Kind regards Warren
This is an interesting approach. Following is what I came up with to scan a directory and store the filenames into an array ... very similar to your example:
       $listDir = array();
$dir = "../mounts/wd/IDT/IDT/";
        if($handler = opendir($dir)) {
            while (($sub = readdir($handler)) !== FALSE) {
                if ($sub != "." && $sub != ".." && $sub != "Thumb.db") {
                    if(is_file($dir."/".$sub)) {
                        $listDir[] = $sub;
                    }elseif(is_dir($dir."/".$sub)){
                        $listDir[$sub] = $this-
ReadFolderDirectory($dir."/".$sub); }
                }
            }
            closedir($handler);
        }
and this is what I'm trying to implement in order to store the array into a mysql table .. $db = mysql_connect("localhost","usr","pwd"); mysql_select_db("db",$db); $colors=serialize($listDir); //takes the data from a post operation... $sql="INSERT INTO recordings (ID, RECORDING, ADDED) VALUES('','$colors','')"; $result = mysql_query($sql) or die (mysql_error()); I'm not sure if this the best or fastest approach ... but it's running in the background as I write this (I should have tested on a smaller folder). The folder I'm scanning has literally over 80,000 files ... so it's taking LOOONG!! --- Rahul Sitaram Johari Founder, Internet Architects Group, Inc. [Email] sleepwalker@rahulsjohari.com [Web] http://www.rahulsjohari.com

« previous php.general (#301063) next »