We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26994
    • 19 Posts
    I have a snippet that takes the content of a directory (images) and outputs them for a Jquery gallery. Problem is the directory needs to be changed each resource. I created a Text TV, to input the directory on the resource page, but I can’t seem to get that TV to output correctly in the snippet. Here is my code:

    <?php
    $path = 'assets/images/agents/ashley-peterson/4717-linden/';
    $dir_handle = @opendir($path) or die('Unable to open ' . $path);
    $array = array();
    while (($file = readdir($dir_handle)) !== false)
    {
    if ($file != '.' && $file != '..')
    {
    $pathinfo = pathinfo($file);
    if (isset($pathinfo['extension'])) {$ext = strtolower($pathinfo['extension']);} else {$ext = '';}
    if ($ext == 'jpg' || $ext == 'jpeg') {$array[] = $file;}
    }
    }
    if ($array)
    {
    sort($array); // Use rsort for Z-A order
    foreach ($array as $value)
    {
    echo '<img src="' . $path . $value . '" alt="" ?>' . "\n";
    }
    }


    Putting the path in the snippet everything works fine, but I have been unable to get the TV to pass to the snippet. I tried passing the path through the call like [[!getdirectoryimages? $path=’[[*directoryTV ]]’ ]] but I just get an error message that the script can’t find the directory.

    Thanks for your help, it is greatly appreciated!
      • 33968
      • 863 Posts
      Your syntax is a little off. Remember to always use backticks (`), and use &parameter for your snippet parameters. Try this for the snippet call:

      [[!getdirectoryimages? &path=`[[*directoryTV ]]` ]]

      Remove the following from your snippet code and it should work.
      $path = 'assets/images/agents/ashley-peterson/4717-linden/';


      If you want to define a default path, add this to the beginning of your snippet:
      <?php
      $path = isset($path) ? $path : 'assets/images/replace-with-default-path/';
      

      (checks for ’path’ in your snippet call; returns default if not set)
        • 26994
        • 19 Posts
        Quote from: lucas at Mar 28, 2011, 08:46 AM

        Your syntax is a little off. Remember to always use backticks (`), and use &parameter for your snippet parameters. Try this for the snippet call:

        [[!getdirectoryimages? &path=`[[*directoryTV ]]` ]]

        Remove the following from your snippet code and it should work.
        $path = 'assets/images/agents/ashley-peterson/4717-linden/';


        If you want to define a default path, add this to the beginning of your snippet:
        <?php
        $path = isset($path) ? $path : 'assets/images/replace-with-default-path/';
        

        (checks for ’path’ in your snippet call; returns default if not set)

        Thanks for your help. I tried your suggestion, but I just get an error message of: Unable to open /assets/images/agents/ashley-peterson/4717-linden

        When I hard code in the path, it works perfectly but when I try to pass the variable, I get a blank page with the error message. You help is greatly appreciated!

          • 3749
          • 24,544 Posts
          The only thing I see is that the path in your snippet ends with a slash and it looks like your TV doesn’t.

          You could also try using a full URL and changing the TV to: images/agents/ashley-peterson/4717-linden/

          $dir_handle = @opendir($modx->getOption('assets_url') . $path) or die('Unable to open ' . $path);


          If that’s not it, paste your current snippet code and your current tag into a message.
            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
            • 26994
            • 19 Posts
            Quote from: BobRay at Mar 28, 2011, 11:23 PM

            The only thing I see is that the path in your snippet ends with a slash and it looks like your TV doesn’t.

            You could also try using a full URL and changing the TV to: images/agents/ashley-peterson/4717-linden/

            $dir_handle = @opendir($modx->getOption('assets_url') . $path) or die('Unable to open ' . $path);


            If that’s not it, paste your current snippet code and your current tag into a message.


            Still getting the same error message. I tried adding the slash to the TV with no luck. Still get the Unable to Open Message. Here is my current snippet-
            <?php
            $dir_handle = @opendir($modx->getOption('assets_url') . $path) or die('Unable to open ' . $path);
            $array = array();
            while (($file = readdir($dir_handle)) !== false)
            {
            if ($file != '.' && $file != '..')
            {
            $pathinfo = pathinfo($file);
            if (isset($pathinfo['extension'])) {$ext = strtolower($pathinfo['extension']);} else {$ext = '';}
            if ($ext == 'jpg' || $ext == 'jpeg') {$array[] = $file;}
            }
            }
            if ($array)
            {
            sort($array); // Use rsort for Z-A order
            foreach ($array as $value)
            {
            echo '<img src="' . $path . $value . '" alt="" ?>' . "\n";
            }
            }


            Here is my call - [[!getdirectoryimages? &path=`[[*directoryTV ]]`]]

            Thanks again for your assistance. I am beating my head over this. Once I have completed this we will finally be able to launch.
              • 3749
              • 24,544 Posts
              Did you check the permissions on the directory?
                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
                • 33968
                • 863 Posts
                Also, how is the path stored in your TV? No input filters or anything?

                Try adding just the straight [[*directoryTV]] placeholder to your template and see if it spits out the correct path. At least that will rule out any problem with the TV.

                  • 33968
                  • 863 Posts
                  Ah! You have an apostrophe (’) missing after $value . in this line:
                              echo '<img src="' . $path . $value . " alt="" />' . "\n";
                  


                  Paste this into your snippet and you should be there:
                  <?php
                  $dir_handle = @opendir($path) or die('Unable to open ' . $path);
                  
                  $array = array();
                      while (($file = readdir($dir_handle)) !== false)
                          {
                          if ($file != '.' && $file != '..')
                              {
                              $pathinfo = pathinfo($file);
                              if (isset($pathinfo['extension'])) {$ext = strtolower($pathinfo['extension']);} else {$ext = '';}
                              if ($ext == 'jpg' || $ext == 'jpeg') {$array[] = $file;}
                              }
                          }
                          if ($array)
                              {
                              sort($array); // Use rsort for Z-A order
                              foreach ($array as $value)
                              {
                              echo '<img src="' . $path . $value . '" alt="" />' . "\n";
                              }
                          }
                  


                  It really helps to develop your snippets in an external editor - you’ll catch those syntax slips that get through easily in the modx manager.