Page 1 of 1
file scan
Posted: Tue Mar 23, 2004 5:09 pm
by Infinity
I need to list my dirs and the files contained in them I can list the dirs on my E: drive but i need to list the files in the dirs before the next dir is output to the screen this is the code i use to list the dirs
Code: Select all
<?php
$mydir = dir('e:');
while(($file = $mydir->read()) !== false) {
if(is_dir($mydir->path.$file)) {
echo "(D) $file<BR>";
}}
?>
and this is the output i get
Code: Select all
(D) Albums - ALBW(Un-Wrapped)
(D) Albums - Non ALBW
(D) ALBW Albums
(D) apps
(D) Artist vs Artist
(D) Coverz
(D) Megamixes
(D) mpg
(D) new mp3's
(D) RECYCLER
(D) Singles
(D) sorted Albums
(D) System Volume Information
(D) Unknown artists
(D) US #1s 1956-2001
when the while loop gets the first dir and before it gets the second i want it to list all the files,subdirs and files in the subdirs before it outputs the second dir and so on.
what im trying to do is scan my drives for mp3 files and import them into mysql database. I know there are progs to do this but i cant find one to do exactly what i want.
I need it to insert the following
filename (obviously)
tag info (most do this)
complete path to the file (for easy linking)
Posted: Tue Mar 23, 2004 6:41 pm
by McGruff
Here's a couple of classes which might be adapted to your needs.
The second class, FileSystemList will need an extra method or two (call in _processItem() method) to build whatever kind of list you need.
Code: Select all
<?php
/*
CLASS DeepIterator
Tree-type structures such as a computer filesystem have a series of nested "nodes" (the folders). The DeepIterator template contains the functionality to loop through everything in and below the given start node right down to the very end of each branch in a tree of some kind. Extend the template to carry out operations on a specific type of tree.
*/
class DeepIterator
{
/*
param (string) $start_node
The tree node from which to start: this, and all
sub-nodes at lower levels, will be looped over.
*/
function DeepIterator($start_node)
{
$this->tree = array($start_node);
}
/*
"Destructive looping" (array_shift) ensures that folders are not processed twice.
*/
function start()
{
while(!is_null($node = array_shift($this->tree)))
{
$this->_processNode($node);
}
}
//////////////////////////////////////////
// PRIVATE //
//////////////////////////////////////////
function _processNode($node)
{
die('Method not implemented.' . __FILE__ . ' | line ' . __LINE__);
}
/*
return (bool)
*/
function _isNode()
{
die('Method not implemented.' . __FILE__ . ' | line ' . __LINE__);
}
function _addNode($node)
{
$this->tree[] = $node;
}
}
///////////////
// END CLASS //
///////////////
class FileSystemList extends DeepIterator
{
/*
param (array) $target_dir
- the folder to backup; no trailing slash!
*/
function FileSystemList($target_dir)
{
$this->target_dir = $target_dir;
Parent::DeepIterator($target_dir);
}
//////////////////////////////////////////
// PRIVATE //
//////////////////////////////////////////
/*
param (string) $path
*/
function _processNode($node)
{
if ($handle = opendir($node))
{
while (false !== ($item = readdir($handle)))
{
$this->_processItem($node, $item);
}
} else {
die('opendir failure' . __FILE__.' | '.__LINE__);
}
closedir($handle);
}
/*
return (bool)
*/
function _isNode($node)
{
return is_dir($node);
}
/*
param (string) $node - current folder
param (string) $item - current item, read from $node
*/
function _processItem($node, $item)
{
if($item != '.' and $item != '..')
{
$path = $node . '/' . $item;
if($this->_isNode($path))
{
$this->_addNode($path);
// .. and add dir ($node) to list
} elseif(is_file($path)) {
// add file to list
}
}
}
}
///////////////
// END CLASS //
///////////////
?>
Posted: Thu Mar 25, 2004 7:59 am
by Infinity
thanx Mc Gruff
Ive just found a script that is perfect for what i need
Dirlister 2.1 by Gary crowhurst if any ones interested you can dowload it from Gary's home page
http://www.knightcommander.karoo.net
or from
http://www.hotscripts.com