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 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@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