2009/8/25 Ralph Deffke <[email protected]>:
> I would say
> foreach( $dirTree as $key => $value ){
> echo $key ."<br>";
> foreach( $value as $v){
> echo $v ."<br>;
> }
> }
>
> something like that
>
> [email protected]
> "Tom Chubb" <[email protected]> wrote in message
> news:[email protected]...
> Hi gang,
> I'm trying to create a script to read the files in a folder (approx
> 2000) and get the filename, path and last modified date in a tabulated
> format to copy into excel. (We have been issued a CD and need to get
> all files documented and assigned to an owner.)
>
> I've tried loads of different scripts but can't get them working with
> all the features.
> I think the best one to work with is this (although I'm having
> problems getting the date but don't worry about that at the moment)
>
> <?
> error_reporting(E_ALL);
> ini_set('display_errors', true);
> function getDirectory($path = '.', $ignore = '') {
> $dirTree = array ();
> $dirTreeTemp = array ();
> $ignore[] = '.';
> $ignore[] = '..';
>
> $dh = @opendir($path);
>
> while (false !== ($file = readdir($dh))) {
>
> if (!in_array($file, $ignore)) {
> if (!is_dir("$path/$file")) {
>
> $dirTree["$path"][] = $file;
>
> } else {
>
> $dirTreeTemp = getDirectory("$path/$file", $ignore);
> if (is_array($dirTreeTemp))$dirTree =
> array_merge($dirTree, $dirTreeTemp);
> }
> }
> }
> closedir($dh);
>
> return $dirTree;
> }
>
> $ignore = array('.htaccess', 'error_log', 'cgi-bin', 'php.ini',
> '.ftpquota');
>
> $dirTree = getDirectory('./Tender', $ignore);
> ?>
> <pre>
> <?
> print_r($dirTree);
> ?>
> </pre>
>
> <?php
> getdirectory('./Tender');
> //or
> //get_dir_iterative(/*etc.*/);
> ?>
>
>
>
>
>
>
> Here is an example of what I'm getting out from the $dirTree array:
>
> Array
> (
> [./Tender] => Array
> (
> [0] => 9216_100_REV_V1.0_bound.dwg
> )
>
>
> [./Tender/Tender Docs] => Array
> (
> [0] => BAA Works Terms v1.1 (22.05.08).pdf
> [1] => Contents of Volumes 1 and 2.pdf
> [2] => Cover Letter and Instructions.doc
>
> [3] => Form of Tender.doc
> )
>
> [./Tender/Tender Docs/Health and Safety Questionnaire] => Array
> (
> [0] => NT Baggage Tender Questionaire rev2.xls
> )
>
>
> [./Tender/Tender Docs/NTB BH Lighting] => Array
> (
> [0] => 3J-B-1 PIR.xls
> [1] => 3J-B-2B PIR.xls
> [2] => 3J-B-2R PIR.xls
> [3] => 3J-B-3R PIR.xls
>
> [4] => 3J-D PIR.xls
> [5] => 4G-G PIR.xls
> [6] => 4J-B-1B PIR.xls
> [7] => 4J-B-1R PIR.xls
> [8] => 4J-B-2B PIR.xls
> [9] => 4J-B-2R PIR.xls
>
> [10] => 4J-B-4 PIR.xls
> [11] => 5G-G PIR.xls
> )
>
> I'm having problems getting my head round how to get access the array
> data so that I can format it how I want, eg:
>
> Folder Filename
> Tender 9216_100_REV_V1.0_bound.dwg
> Tender/Tender Docs BAA Works Terms v1.1 (22.05.08).pdf
> Tender/Tender Docs Contents of Volumes 1 and 2.pdf
>
> etc.
>
> I'm trying to do this at work (php is a hobby and this is the first
> time I've tried to use it in my electrical engineering job) in notepad
> without any code highlighting, etc. and tearing my hair out to try and
> avoid going through the CD manually!
>
> Could anybody please help or let me know which function I need to read
> up on? I've tried countless searches on array formatting, etc and not
> getting anywhere.
>
> Thanks in advance,
>
> Tom
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
Ended up tidying it up and using Ralph's idea and now working nicely.
(Need to 'view source' to see it properly as I was having issues
copying into excel but this is fine for what I'm trying to do.)
Now I'm having problems getting the file date.
I'm using: $modDate["$file"][] = date ("d m Y", filemtime($file));
and trying to merge it into the array but I'm missing something. Any
ideas?
Refer to lines 5, 14, 17,
New code:
<?
function getDirectory($path = '.', $ignore = '') {
$dirTree = array ();
$dirTreeTemp = array ();
$modDate = array ();
$ignore[] = '.';
$ignore[] = '..';
$dh = @opendir($path);
while (false !== ($file = readdir($dh))) {
if (!in_array($file, $ignore)) {
if (!is_dir("$path/$file")) {
$dirTree["$path"][] = $file;
$modDate["$file"][] = date ("d m Y",
filemtime($file));
} else {
$dirTreeTemp = getDirectory("$path/$file", $ignore);
if (is_array($dirTreeTemp))$dirTree =
array_merge($dirTree, $dirTreeTemp, $modDate);
// if (is_array($dirTreeTemp))$dirTree =
array_merge($dirTree, $dirTreeTemp);
}
}
}
closedir($dh);
return $dirTree;
}
$ignore = array('.htaccess', 'error_log', 'cgi-bin', 'php.ini', '.ftpquota');
$dirTree = getDirectory('./Tender', $ignore);
getdirectory('./Tender');
echo "Gatwick Tender Documents\n";
foreach( $dirTree as $key => $folder ){
//echo $key . "\n"; //Don't need folders as they're shown with the files
foreach( $folder as $file){
echo str_replace("./Tender", "", $key) . "/\t" . $file . "\n";
}
}
?>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php