• Snippet return for directory contents#

  • Pleth Reply #1, 4 years ago

    Reply
    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?


  • BobRay Reply #2, 4 years ago

    Reply
    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



  • powersitedesign Reply #3, 4 years ago

    Reply
    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


  • Jay Gilmore Reply #4, 4 years ago

    Reply
    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.


  • Pleth Reply #5, 4 years ago

    Reply
    Thanks guys, we appreciate the help.


  • Pleth Reply #6, 4 years ago

    Reply
    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;
    ?>
    


  • Neutron Reply #7, 2 years, 10 months ago

    Reply
    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="ylightbox[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 ylightbox with it..

    Hope someone can help me! hehehe I dont want to make the thumbs manually!.. thx


  • Neutron Reply #8, 2 years, 10 months ago

    Reply
    I got the error, extra quotes...
                 $output .= '<li><a href="'.$dir.''.$value.'" rel="ylightbox[group1]"> <img src="[[MiniPhoto? &file=`'.$dir.''.$value.'` &dir=`mini` &height=`130`]]" /></li>';
    


    thats the right way,

    Thx