We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 23018
    • 353 Posts
    phpFileTree v0.0.1
    A snippet recursively listing all included directories and files within a specific directory.

    Attention: WORK IN PROGRESS!

    A year ago "pleth" posted a neat snippet that creates "unordered lists based on documents in a given directory"
    (http://modxcms.com/forums/index.php/topic,30700.msg186267/topicseen.html#msg186267).

    Among other things, this could be useful for
    - building javascript image galleries,
    - for generating a mediaplayer playlist,
    - listing pdf documents in a directory.
    - ...

    After pondering about the opportunities such a script can offer, I though it would be a good idea to power this idea a bit up...

    In the past I have used "PHP File Tree" by Cory S.N. LaViska (http://abeautifulsite.net/notebook/21) for similar tasks. Go and check it out to see what can be achieved with it. Especially have a look at those demo pages: http://abeautifulsite.net/notebook_files/21/demo/demo_classic.php and http://abeautifulsite.net/notebook_files/21/demo/demo_jquery.php

    Because Cory doesn’t mind sharing his humble work with other people...

    ...you can use it however you want with or without attribution, so long as you understand and accept that it is offered as-is, with no warranty whatsoever. Also, we obviously won’t be able to provide support for it.

    Thanks Cory!

    During this weekend I’ve frankensteined Cory’s script and added some additional limbs and organs. You can find it attached to this post.

    Attention: You are free to use this snippet in every way you like but like Cory I’m offering it as it is and there is no warranty for it too . If you want to make use of it, do it. But please BE CAREFUL about what kind of information you are offering to the public.

    Usage:

    1. Snippet call:
    ----------------

    [!phpFileTree? &directory=`images` &return_link=`[link]` &addFile=`jpg,png,gif` &removeFile=`png` &ignore=`thumb` !]

    2. Parameters:
    --------------
    &directory=`images`
    The directory you want to start your journey from. For security reasons, the assets directory is hardwired into the snippet.

    &return_link=`[link]`
    Control how the URL to a resource will look like. [link] will just create an ordinary link to each file.

    &addFile=`jpg,png,gif`
    If not empty INCLUDE only files with the listed extensions. Just use a comma to separate multiple filetypes. Just add the extensions, don’t use a "." in front.

    &removeFile=`png`
    Same as above, but If not empty, EXCLUDE only files with a certain extension.

    &ignore=`thumb`
    Remove files from list that contain a specific string within their filenames. To remove all thumb_*.* from your list, just add thumb to this parameter.

    Note: For more information have a look at the comments inside the snippet.

    3. Tips and Tricks
    ------------------
    - Until I have added some templating functionality (can I hear someone volunteering for this task?) you have to change parts of the snippet’s php code. If you don’t know how to do this post your question and I will try to help you.
    - Controlling depth of list. Right now there is no way to prevent the snippet from going all the way down to the bottom of a directory. If this is a problem for you, you have to point it to a folder that only has the included directories you want to list.

    ToDo (in no particular order):
    ------------------------------
    - Remove inline styling for debugging the snippet.
    - Clean up messy code.
    - Add some security features to prevent (accidental) abuse.
    - Add some generic filetypes (Example: &addFile=`images` could automatically add "jpg,png,tiff,jpeg" )
    - Create a mechanism for templating the list generation (outer,inner).
    - Add a variable to control the depth of the recursive search
    - Add parameters to remove specific directories from your result.
    - Sorting the results should also be possible.
    - ...

    Have fun.

    pepebe

    P.S. I’ll appreciate any advice, support or feedback for this script. I can imagine many uses for this snippet and overwhelming public demand might even move me to develop it into a fullscale plugin...



      Homepage: pepebe.de | MODX snippets (and other stuff) at github: https://gist.github.com/pepebe
      • 23018
      • 353 Posts
      I have just updated the script.

      Download phpFileTree_v0.0.2 from the first post.

      Version/Date 0.0.2/Oct, 12th 2009

      * - Added generic filetypes (image, media, flash, document). &addFile=`image` will automatically include a wide range of predefined image filetypes.
      * - Added ability to exclude directories (and subdirectories) from list
      * - Added URL to directory names so they can redirect to themselves (useful to add custom classes to an "active dir - see below).
      * - Added class "activeDir" to currently active directory, so you can use css to switch on/off folders/files.
      * - Skipped the idea of sorting directory output for now.
      * - html construction is now done within three functions "outerTpl", "fileTpl" and "dirTpl".
      * You’ll find those fileas near the top of the script. Right now there is no option to move those templates out of the script to their own chunks.

      Found a bug that I don’t understand. Perhaps somebody can tell me whats wrong.

      Try ignore=`.`

      From my point of view this should get rid of .htaccess or .thumb but not from test.gif

      Unfortunately it will get rid of EVERYTHING.

              // Remove files containing a specific string within their filenames.
              if( !empty($ignore[0]) ) {
                  foreach ( array_keys($file) as $key ) {
                      if( !is_dir("$directory/$file[$key]") ) {
                          $fileName = substr($file[$key],0,strrpos($file[$key],'.'));
                          // check whether $filename contains a specific string
                          foreach(array_keys($ignore) as $par ) {                            
                              if(eregi($ignore[$par],$fileName)) {
                              unset($file[$key]);
                              }
                          }
                      }
                  }
              }
      


      First I thought there was something wrong with this line:

      $fileName = substr($file[$key],0,strrpos($file[$key],'.'));


      But this doesn’t seem to be the case.

      I’m tired.

      Do you have any clue?

      Regards,

      pepebe
        Homepage: pepebe.de | MODX snippets (and other stuff) at github: https://gist.github.com/pepebe
        • 23018
        • 353 Posts
        Oh, sorry I have just noticed this thread would make MUCH MORE SENSE within "In Development" forum.

        Is anybody here who can do this for me?

        Kind regards,

        pepebe

        P.S. BTW The "Report to moderator" link doesn’t work. I get a message telling me, that I can’t report my own work. This "wouldn’t make sense". Well, in this case it would, wouldn’t it?

          Homepage: pepebe.de | MODX snippets (and other stuff) at github: https://gist.github.com/pepebe
          • 23018
          • 353 Posts
          Update: No new release, but I want to let you know that there is an issue with the snippet and YAMS.

          For more details have a look at this post: http://modxcms.com/forums/index.php/topic,36513.msg247060.html#msg247060

          As soon as I have found out what to do about that problem, I’ll let you know.

          Regards,

          pepebe


          Solved in Version 0.0.4a!
            Homepage: pepebe.de | MODX snippets (and other stuff) at github: https://gist.github.com/pepebe
            • 23018
            • 353 Posts
            Uploaded an update to the first post.

            Just a view cosmetic changes and some bug fixing.

            * 0.0.3/Oct, 16th 2009
            * - Removed a bug causing lists to be nested the wrong way (did anybody notice? If yes why don’t you tell me?)
            * - Added function that displays a text message if a directory is empty (Note: Move Message to a config array and think about yams integration )
            * - According to w3c html validator we are 100% on the "green" side.
            * - Moved functions to phpFileTree.inc.php. Just copy the included folders to your modx directory and then copy the contents of phpFileTree.txt to a snippet and name it "phpFileTree".

            Note: There is still a problem regarding yams. I don’t recommend to use this pluging in a multi language solution until I have found out how to get rid of it.

            Regards,

            pepebe

            P.S. Apart from some minor improvements I don’t know now what to do next. Do you have any recommendations?
              Homepage: pepebe.de | MODX snippets (and other stuff) at github: https://gist.github.com/pepebe
              • 36624
              • 535 Posts
              hey this is a nice snippet !

              something i know would be amazing to have is the ability to drag/drop files to another folder. grin


                CTRL+SHIFT+U - Clear Cache
                CTRL+SHIFT+H - Hiding Heft Panel
                CTRL+SHIFT+N - Fast Create Resource
                CTRL+ALT+P - Preview Recource (in edit resorce window)
                CTRL+ALT+S - Save
                • 23018
                • 353 Posts
                Thanks Manu

                Well I wrote this snippet to create a list of images for a lightbox gallery, but it would be possible to develop it even further.

                I neither like the way the "manage file" module nor the way FCK editor’s "Ressources Browser" (RB) handles file management so it might be well worth it to look for an alternative.

                From my point of view, I don’t see the point why we have multiple things that do about the same thing (listing directories) doing it in a totally different way.

                Example: RB becomes awfully slow in nested structures with a lot of images and folders. I can’t stand the fact, that I can’t go to images/id/34/gallery within a few seconds, because every time I click on a folder the script will create a complete image gallery. I would like to have something like "nautilus" (ubuntu) or "file manager" (windows).

                A simple filetree to the left, a "whats inside" view to the right. In the top-left corner a breadcrumb telling me where I am (I know the filetree does the same thing...) and in the top left corner a dropdown list to change the view from "simple" to "detailed" to "gallery". Bookmarks would be nice too wink

                Unfortunately I don’t have the time right now to start something like that.

                I can’t remember how it works in MODx Revo. Is there some improvement in this area?

                Regards,

                pepebe

                  Homepage: pepebe.de | MODX snippets (and other stuff) at github: https://gist.github.com/pepebe
                  • 23018
                  • 353 Posts
                  Thanks to inspiring input by Laurent Toulemonde I have added a new feature to the code and along the way got rid of a stupid microbug.

                  Now you can choose wheter files are display with extenion or without.

                  &showExtension=`1` will display any file with its individual extension.
                  &showExtension=`0` (default) will just display the filename.

                  BTW. In case you haven’t noticed.

                  *Each and every list item has it’s individual class based on its extension. Therefore you can use the css attribute list-style-image: url() to layout individual list items.
                  *Also, you may use TVs to insert values into the snippet parameters.

                  Example: Use a select TV to define a range of possible paths for the directory parameter.

                  Snippet parameter: &directory=`[*choose_directory*]`
                  Choose_directory TV: Manuals==media/manuals||Novels==media/novels||Newsletters==/media/newsletters

                  Voila, now you can choose among different paths without any need to change your snipet code.


                  As ususal, you will find the latest snippet at the bottom of the first post.

                  Regards,

                  pepebe
                    Homepage: pepebe.de | MODX snippets (and other stuff) at github: https://gist.github.com/pepebe
                    • 20879
                    • 13 Posts
                    Great script.

                    One issue though: If the file name has spaces in it, then you will get error 404 if you click on the link to the file, as the spaces will be replaced with "+" signs instead of "%20".

                      • 23018
                      • 353 Posts
                      Quote from: nahawand at Nov 13, 2009, 01:10 PM

                      Great script.

                      One issue though: If the file name has spaces in it, then you will get error 404 if you click on the link to the file, as the spaces will be replaced with "+" signs instead of "%20".



                      Thanks nahawand, I have missed that point sad

                      I have to dig deeper into this problem but for now I have fixed that issue with some search’n replace. This should do for now.

                      As usual, you will fin the latest version (0.0.5) below the first post.

                      Regards,

                      pepebe
                        Homepage: pepebe.de | MODX snippets (and other stuff) at github: https://gist.github.com/pepebe