We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 23571
    • 223 Posts
    Lately I have used jCarousel and Modx for a site with a photo gallery and it works quite well. Since it works with a simple unordered list on the gallery page I got to thinking that it would be nice to have a snippet that could return the contents of an assets directory in that list item format.

    It seems this would create a gallery within Modx that the CMS user could manage by simply uploading to/deleting from a specified directory. Has anyone done anything like this?
      • 3749
      • 24,544 Posts
      I’ve done something similar (if I’m understanding you). It’s a fairly simple process.

      I’ve adapted some code I had laying around. This is very quick and dirty code and untested, but should get you started.

      <?php
      
      // Shows all files in the directory,
      
      $newline = "<br/>";
      
      $dir = $modx->config['base_path'].'assets/yourfiles'; // set path to files
      
      $dir_array = array(); // main array - contains all file names in directory
      
      
      // open directory and parse file list 
      
      if (is_dir($dir)) { 
      
         if ($dh = opendir($dir)) { 
      
            // iterate over file list to create full directory array
      
            while (($filename = readdir($dh)) !== false) { 
      
                if (($filename != ".") && ($filename != "..") && ($filename != "WS_FTP.LOG")) { // skip self, parent, and ftp log
      
      		  	$dir_array[] = $filename; // add the filename to the array
                } 
            } 
            closedir($dh);  // close directory 
         }
      
         natsort($dir_array); // sort in ascending order -- delete if you don't need them sorted.
         // $dir_array = array_reverse($dir_array, false); // reverse array (descending) if needed.
      
         $n = count($dir_array); // total number of files -- might want this for something
      
          $output = "";
      
          $output .= "<ul>"
      
          foreach ($dir_array as $value) {  // iterate through array of filenames.
      
               $output .= '<li>'.$value.'</li>';
      
          } 
      
          $output .='</ul>';
          
      }
      
      return $output;
      
      ?>


      BTW, you might look at the MaxiGallery snippet if you haven’t already. It does all this for you and makes for much easier and more full-featured gallery management by the users (e.g. picture order, uploading and deleting, titles, descriptions, dropshadows, masks, slideshow, etc).

      Hope this helps,

      Bob

        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 23775
        • 1 Posts
        Hey BobRay -

        I am working w/ Greg on this and we think we almost have it. The only thing we want to do now is to exclude the .thumb_ file prefix so the thumbnails don’t show, do you have any suggestions for that?

        We tried this,

        if (($filename != ".") && ($filename != "..") && ($filename !=
        "WS_FTP.LOG") && (!preg_match(’/^\.*thumb_$/’, $filename ) ) { // skip
        self, parent, and ftp log and thumb prefix

        and it didn’t seem to exclude them,
        thanks for your help in advance.

        Cotton Rohrscheib, Partner
        Pleth Networks, LLC
        http://www.pleth.com
        • I think that @pleth has it working now. He found me on twitter and I gave him the correct pattern to add to BobRay’s post.

          I changed the following:
          if (($filename != ".") && ($filename != "..") && ($filename != "WS_FTP.LOG")) { // skip self, parent, and ftp log

          to
          if (($filename != ".") && ($filename != "..") && ($filename != "WS_FTP.LOG") && (!preg_match('/^.thumb_/', $filename))) { // skip self, parent, and ftp log

          And it works.

          The pattern just checks to see if the filename starts with .thumb_ and if it does skip it.
            Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. BlogTwitterLinkedInGitHub
            • 23571
            • 223 Posts
            Thanks guys, we appreciate the help.
              • 23571
              • 223 Posts
              Just thought I would post this to let you know where we ended up on this. I realize there are other solutions available and am planning on diving into MaxiGallery next but this solution proved sufficient as well as a good exercise. Maybe this will be useful to someone as I think it could be applied to other types of documents. Once again, thanks.

              <?php
              /* -------------------------------------------------------------
              :: Snippet: Returns Directory Contents
              ----------------------------------------------------------------
                  Short Description: 
                     Returns Directory Contents, excludes self, parent, ftp log and thumbnail with prefix (.thumb_)
              
                  Date:
                      05/16/2008
              
              ----------------------------------------------------------------
              :: Example Call             
              ----------------------------------------------------------------
              [!directory? &Location=`yourdirectory`!]
                  A call that describes the directory inside of assets/images/ that you want called in.
              
              ------------------------------------------------------------- */
              
              // Shows all files in the directory (assumes you are in assets/images/),
              
              $newline = '';
              
              $dir = $modx->config['(site_url)'].'assets/images/'.$Location.'/'; // set path to files
              
              $dir_array = array(); // main array - contains all file names in directory
              
              
              // open directory and parse file list 
              
              if (is_dir($dir)) { 
              
                 if ($dh = opendir($dir)) { 
              
                    // iterate over file list to create full directory array
              
                    while (($filename = readdir($dh)) !== false) { 
              
                        if (($filename != ".") && ($filename != "..") && ($filename !="WS_FTP.LOG") && (!preg_match('/^.thumb_/', $filename))) { // skip self, parent, and ftp log and thumb prefix
              		  
              	       $dir_array[] = $filename; // add the filename to the array
                        } 
                    } 
                    closedir($dh);  // close directory 
                 }
              
                 natsort($dir_array); // sort in ascending order -- delete if you don't need them sorted.
                 // $dir_array = array_reverse($dir_array, false); // reverse array (descending) if needed.
              
                 $n = count($dir_array); // total number of files -- might want this for something
              
                  $output = '';
              
                  $output .= '<ul id="mycarousel" class="jcarousel-skin-tango">';
              
                  foreach ($dir_array as $value) {  // iterate through array of filenames.
              
                       $output .= '<li><img src="'.$dir.''.$value.'" /></li>';
              
                  } 
              
                  $output .='</ul>';
                  
              }
              
              return $output;
              ?>
              
                • 21295
                • 22 Posts
                I know this is a bit old, but is there a way I can thumbs for this snipped? can I call another snipped inside like miniphoto? having something like this :
                         $output .= '<li><a href="'.$dir.''.$value.'" rel="sexylightbox[group1]"> <img src=[!MiniPhoto? &file=`"'.$dir.''.$value.'"` &dir=`mini` &height=`130`!] /></li>';
                


                is this possible? or there is another way to make thumbs on the fly inside a snipped?
                I’ve tryed this and the thumbs are not generated, folder is in 777,.. any idea? (I know there is maxigallery but I want to use jcarousel and sexylightbox with it..

                Hope someone can help me! hehehe sad I dont want to make the thumbs manually!.. thx smiley
                  • 21295
                  • 22 Posts
                  I got the error, extra quotes...
                               $output .= '<li><a href="'.$dir.''.$value.'" rel="sexylightbox[group1]"> <img src="[[MiniPhoto? &file=`'.$dir.''.$value.'` &dir=`mini` &height=`130`]]" /></li>';
                  


                  thats the right way,

                  Thx
                    • 38309
                    • 40 Posts
                    I know this is an old post, but Im trying to find something to help me display a list of files, from an external ftp server. i.e. a NAS with web access.
                    I did look at FileLister but it didnt seem to want to read from the external ftp address.

                    could the above script be modified ?

                    this resource will be behind a login so im not worried about security issues.