Jump to content

Recommended Posts

so i have a function that returns a list of all of the files and directories, sub directories and sub directory files into an array, but I am having a little trouble formatting the output.

 

Here is the arrays output using print_r:

0 -> AIA DOCS

1 -> AIA DOCS/IPD_Guide_2007.pdf

2 -> ENERGY

3 -> ENERGY/AEDG_K12.pdf

4 -> ENERGY/AEDG_SmallOfficeBuildings.pdf

5 -> MANAGEMENT

6 -> MANAGEMENT/Article_ Influence_ Connect...pdf

 

The goal here is to have a list of all of our company resources, but instead of just a running list like you see above, I would like to have the list organized into expanding folders.

so you would essentially see a list of sub directories and when you click on the sub directory, it expands and shows the files within.

- AIA DOCS

to

 

+ AIA DOCS

--IPD_Guide_2007.pdf

--file2.pdf

--file3.pdf

--etc..

 

So how can I take the single layer array and format it this way?

 

Link to comment
https://forums.phpfreaks.com/topic/166793-array-output-formatting/
Share on other sites

A better question is how to get your data into a better format to begin with. Instead of having allt he folders/files in a signle array and having to use string manipulation the data shoud be in a multidimensional array.

 

Edit: I posted a function, but had to withdraw as I need to review it.

You could list all files at once (and using javascript only display one branch at a time) or display upon request:

 

Display upon request:

$expand = $_POST['expand'];//don't forget to validate
function create_tree($directory) {
    global $expand;
    $files = scandir($directory);
    print '<ul>';
    foreach ($files as $file) {
        $basename = basename($file);
        if (is_dir($file)) {
            print "<li><a href=\"currentfile.php?expand=$file\"><img src=\"images/folder.png\" alt=\"folder\"> $basename</a></li>";
            if ($file === $expand) {
                print '<li>' . create_tree($expand) . '</li>';
            }
        } else {
            print "<li><a href=\"currentfile.php?expand=$file\"><img src=\"images/file.png\" alt=\"file\"> $basename</a></li>";
        }
   }
   print '</ul>';
}

create_tree(realpath('/path/to/expandable/directory'));

 

This code is only intended to show you how you can faciliate this and is not intended for production use. A better aproach would be using the composite pattern.

 

Edit: preferrably use mjdamato's solution, although I have one remark: why not create constants for the $to_return types? like DTA_RETURN_FILES?

honestly I dont know how to populate a multidimensional array. i am using a code I found on the net to list the folders and have tried to make a multidimensional array, but its all new to me. My php experience is pretty basic, so just single layer arrays are new to me.

 

Here is the code i am currently using:

<?php

function getDirectoryListing($dirname, $sortorder = "a", $show_subdirs = 1, $show_subdirfiles = 0, $exts = "all", $ext_save = 1) {
// This function will return an array with filenames based on the criteria you can set in the variables
// @sortorder : a for ascending (the standard) or d for descending (you can use the "r" for reverse as well, works the same)
// @show_subdirs : 0 for NO, 1 for YES - meaning it will show the names of subdirectories if there are any
// Logically subdirnames will not be checked for the required extentions
// @show_subdirfiles : 0 for NO, 1 for YES - meaning it will show files from the subdirs
// Files from subdirs will be prefixed with the subdir name and checked for the required extentions.
// @exts can be either a string or an array, if not passed to the function, then the default will be a check for common image files
// If exts is set to "all" then all extentions are allowed
// @ext_save : 1 for YES, 0 for NO - meaning it will filter out system files or not (such as .htaccess)

  if (!$exts || empty($exts) || $exts == "") {
       $exts = array("jpg", "gif", "jpeg", "png", "doc", "xls", "pdf", "tif");
   }
   if ($handle = opendir($dirname)) {
       $filelist = array();
       while (false !== ($file = readdir($handle))) {

           // Filter out higher directory references
           if ($file != "." && $file != "..") {
               // Only look at directories or files, filter out symbolic links
               if ( filetype ($dirname."/".$file) != "link") {
                   // If it's a file, check against valid extentions and add to the list
                   if ( filetype ($dirname."/".$file) == "file" ) {
                       if (checkFileExtention($file, $exts, $ext_save)) {
                                       $filelist[] = $file;
                       }
                   }
                   // If it's a directory and either subdirs should be listed or files from subdirs add relevant names to the list
                   else if ( filetype ($dirname."/".$file) == "dir" && ($show_subdirs == 1 || $show_subdirfiles == 1)) {
                       if ($show_subdirs == 1) {
                           $filelist[] = $file;
                       }
                       if ($show_subdirfiles == 1) {
                           $subdirname = $file;
                           $subdirfilelist = getDirectoryListing($dirname."/".$subdirname."/", $sortorder, $show_subdirs, $show_subdirfiles, $exts, $ext_save);
                           for ($i = 0 ; $i < count($subdirfilelist) ; $i++) {
                               $subdirfilelist[$i] = $subdirname."/".$subdirfilelist[$i];
                           }
                           $filelist = array_merge($filelist, $subdirfilelist);
                       }

                   }

               }
           }
       }
       closedir($handle);

       // Sort the results
       if (count($filelist) > 1) {
           natcasesort($filelist);
           if ($sortorder == "d" || $sortorder == "r" ) {
               $filelist = array_reverse($filelist, TRUE);
           }
       }
       return $filelist;
   }
   else {
       return false;
   }
}

function checkFileExtention($filename, $exts, $ext_save = 1) {
   $passed = FALSE;
   if ($ext_save == 1) {
       if (preg_match("/^\./", $filename)) {
           return $passed;
       }
   }
   if ($exts == "all") {
                   $passed = TRUE;
       return $passed;
   }
   if (is_string($exts)) {
       if (eregi("\.". $exts ."$", $filename)) {
                       $passed = TRUE;
           return $passed;
       }
   } else if (is_array($exts)) {
       foreach ($exts as $theExt) {
           if (eregi("\.". $theExt ."$", $filename)) {
               $passed = TRUE;
               return $passed;
           }
       }
   }
   return $passed;
}
?>

 

Any ideas on how to edit this to make it a multidimensional array?

 

Thanks

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.