Jump to content

Php Array Question


deathdeyfer2002

Recommended Posts

All-

 

Here is the simple script I am trying to use :

http://lixlpixel.org/recursive_function/ph...directory_scan/

 

 

How do I pull the data out of there? Looks like it is being stored in an array.

 

I have tried many variations.

 

Here is some of what I have tried.

$filestructure = scan_directory_recursively('/var/www/');

echo $filestructure['content'{1}];

 

Can someone please please help me?

 

Thanks

Defyer!

Link to comment
https://forums.phpfreaks.com/topic/177497-php-array-question/
Share on other sites

01 <?php
02
03 // ------------ lixlpixel recursive PHP functions -------------
04 // scan_directory_recursively( directory to scan, filter )
05 // expects path to directory and optional an extension to filter
06 // of course PHP has to have the permissions to read the directory
07 // you specify and all files and folders inside this directory
08 // ------------------------------------------------------------
09
10 // to use this function to get all files and directories in an array, write:
11 // $filestructure = scan_directory_recursively('path/to/directory');
12
13 // to use this function to scan a directory and filter the results, write:
14 // $fileselection = scan_directory_recursively('directory', 'extension');
15
16 function scan_directory_recursively($directory, $filter=FALSE)
17 {
18     // if the path has a slash at the end we remove it here
19     if(substr($directory,-1) == '/')
20     {
21         $directory = substr($directory,0,-1);
22     }
23  
24     // if the path is not valid or is not a directory ...
25     if(!file_exists($directory) || !is_dir($directory))
26     {
27         // ... we return false and exit the function
28         return FALSE;
29  
30     // ... else if the path is readable
31     }elseif(is_readable($directory))
32     {
33         // we open the directory
34         $directory_list = opendir($directory);
35  
36         // and scan through the items inside
37         while (FALSE !== ($file = readdir($directory_list)))
38         {
39             // if the filepointer is not the current directory
40             // or the parent directory
41             if($file != '.' && $file != '..')
42             {
43                 // we build the new path to scan
44                 $path = $directory.'/'.$file;
45  
46                 // if the path is readable
47                 if(is_readable($path))
48                 {
49                     // we split the new path by directories
50                     $subdirectories = explode('/',$path);
51  
52                     // if the new path is a directory
53                     if(is_dir($path))
54                     {
55                         // add the directory details to the file list
56                         $directory_tree[] = array(
57                             'path'    => $path,
58                             'name'    => end($subdirectories),
59                             'kind'    => 'directory',
60  
61                             // we scan the new path by calling this function
62                             'content' => scan_directory_recursively($path, $filter));
63  
64                     // if the new path is a file
65                     }elseif(is_file($path))
66                     {
67                         // get the file extension by taking everything after the last dot
68                         $extension = end(explode('.',end($subdirectories)));
69  
70                         // if there is no filter set or the filter is set and matches
71                         if($filter === FALSE || $filter == $extension)
72                         {
73                             // add the file details to the file list
74                             $directory_tree[] = array(
75                                 'path'      => $path,
76                                 'name'      => end($subdirectories),
77                                 'extension' => $extension,
78                                 'size'      => filesize($path),
79                                 'kind'      => 'file');
80                         }
81                     }
82                 }
83             }
84         }
85         // close the directory
86         closedir($directory_list); 
87  
88         // return file list
89         return $directory_tree;
90  
91     // if the path is not readable ...
92     }else{
93         // ... we return false
94         return FALSE;    
95     }
96 }
97 // ------------------------------------------------------------
98
99 ?>

Link to comment
https://forums.phpfreaks.com/topic/177497-php-array-question/#findComment-935869
Share on other sites

Still plugging away at this.  With the help from the guys at Hak5.org, I was able to come up with this using the following script

 

$fileselection = scan_directory_recursively('/var/www','mp4');
$count = count($fileselection);
for ($i = 0; $i < $count; $i++) {
    $extractedArray = $fileselection[$i];
    echo $extractedArray['name'] . "<br />";
}

 

Only Problem is that it only is showing the contents of the current directory.  I need to to work on all the sub directories within that current directory.  Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/177497-php-array-question/#findComment-938406
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.