Re: Re: Directory Listing
| From: | Tom Chubb | Date: | Tue, 25 Aug 2009 10:53:20 +0000 |
| Subject: | Re: Re: Directory Listing | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-297182@lists.php.net to get a copy of this message | ||
2009/8/25 Ashley Sheridan <ash@ashleysheridan.co.uk>:
> On Tue, 2009-08-25 at 12:06 +0200, Ralph Deffke wrote:
>> I would say
>> foreach( $dirTree as $key => $value ){
>> echo $key ."<br>";
>> foreach( $value as $v){
>> echo $v ."<br>;
>> }
>> }
>>
>> something like that
>>
>> ralph_deffke@yahoo.de
>> "Tom Chubb" <tomchubb@gmail.com> wrote in message
>>
>> news:4577934f0908250241i195dc691x3f8c552e72791f4e@mail.gmail.com...
>> 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
>>
>>
>>
> Funnily enough, I've had to dust off an old function I wrote for this
> sort of thing this very morning:
>
> function dirTree($dir)
> {
> $dirs = Array();
> if($dh = opendir($dir))
> {
> $dirCount = 0;
> while(false !== ($file = readdir($dh)))
> {
> // ignore the pseudo . and .. directories
> if( $file == '.' || $file == '..')
> {
> continue;
> }
> $path = str_replace('//', '/',
> "$dir/$file");
> if(is_dir($path))
> {
> $dirs[$path] = dirTree($path);
> $dirCount ++;
> }
> if(is_file($path) )
> {
> // process this file
>
> }
> }
> if($dirCount > 0)
> {
> return $dirs;
> }
> else
> {
> return false;
> }
> }
> else
> {
> return false;
> }
> }
>
> $dir = "/home/user/Documents/";
> $tree = dirTree($dir);
>
> The array $tree is a multidimensional array which should make sense to
> look at if you do a print_r() on it!
>
> The code looks a bit messy, and could be improved probably a dozen ways,
> but it does work, and is very easy to mess around with.
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
Thanks Ash,
I've never really worked with arrays and until now I never understood
what a multi-dimensional array was!
I'll try and mess around with it.
Thanks to Ralph also.
Tom