Topic: Snippet return for directory contents  (Read 3950 times)

Pages: [1]   Go Down

#1: 14-May-2008, 08:51 PM


pleth
Posts: 120

WWW
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?
Greg

#2: 14-May-2008, 10:47 PM

Coding Team

BobRay
Posts: 4,935

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

Code:
<?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

MODx info for newbies: http://bobsguides.com/MODx.html

#3: 15-May-2008, 11:10 AM

powersitedesign
Posts: 1

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

#4: 15-May-2008, 12:49 PM

Testers

ganeshXL
Posts: 2,014

true is true

WWW
using strstr or stristr would be easier... http://ch2.php.net/stristr

#5: 15-May-2008, 02:16 PM

Administrator

smashingred
Posts: 1,415

Jay Gilmore

WWW
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:
Code:
if (($filename != ".") && ($filename != "..") && ($filename != "WS_FTP.LOG")) { // skip self, parent, and ftp log
to
Code:
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.

#6: 15-May-2008, 02:36 PM


pleth
Posts: 120

WWW
Thanks guys, we appreciate the help.
Greg

#7: 16-May-2008, 11:07 AM


pleth
Posts: 120

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

Code:
<?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;
?>

Greg

#8: 9-Jul-2009, 03:39 AM

Neutron
Posts: 18

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 :
Code:
         $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

#9: 9-Jul-2009, 05:25 AM

Neutron
Posts: 18

I got the error, extra quotes...
Code:
             $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
Pages: [1]   Go Up
0 Members and 1 Guest are viewing this topic.