Page 1 of 1

unable to access image folder

Posted: Tue Oct 08, 2019 11:35 am
by Sendlingur
I'm trying to display images from this folder
uploads/images/mypicfolder
to do that I'm using this snippet, which I got from https://cmscanbesimple.org/blog/list-images :

Code: Select all

$url = isset($params['/uploads/images/ ']) ? $params['/uploads/images/ '] : '';
$dir = cmsms()->config['root_path'] . DIRECTORY_SEPARATOR . $url;

echo "<ul>\n";
if (is_dir($dir))
{
  if ($dh = opendir($dir))
  {
    while (($file = readdir($dh)) !== false)
    {
      if ( ('file' == filetype($dir . $file)) && (substr($file,0,6) != 'thumb_') )
      {
        echo '<li><a href="'.$url.$file.'" class="fancybox"><img src="'.$url.'thumb_'.$file.'" alt="'.$file.'" /></a>'."</li>\n";      
      }
    }
    closedir($dh);
  }
}
echo "</ul>\n";
I have a custom field for the name of the folder since the user should be able to add different names of folders for each article. (im using the News module)

The custom field is called picfolder.

So in my news detail template I have this tag

{list_images url='{$entry->picfolder}'}

The problem is that I always get a list of some files on the root. but not the image files I want to display.

what am I doing wrong here?

Re: unable to access image folder

Posted: Wed Oct 09, 2019 7:49 am
by Jos
$params['/uploads/images/ '] is not a correct modification of the original code

Re: unable to access image folder

Posted: Wed Oct 09, 2019 9:33 am
by Sendlingur
I'm aware of that, I've tried all kinds of paths but with out any luck.
Could you please guide me on what would be the correct modification to access the image folder?

Re: unable to access image folder

Posted: Wed Oct 09, 2019 4:24 pm
by Jos
Try this:

Code: Select all

$url = isset($params['url']) ? $params['url'] : '';
$dir = cms_join_path(cmsms()->config['image_uploads_path'], $url);

echo "<ul>\n";
if (is_dir($dir))
{
  if ($dh = opendir($dir))
  {
    while (($file = readdir($dh)) !== false)
    {
      if ( ('file' == filetype($dir . $file)) && (substr($file,0,6) != 'thumb_') )
      {
        echo '<li><a href="'.$url.$file.'" class="fancybox"><img src="'.$url.'thumb_'.$file.'" alt="'.$file.'" /></a>'."</li>\n";      
      }
    }
    closedir($dh);
  }
}
echo "</ul>\n";
And use tag: {list_images url=$entry->picfolder}

Re: unable to access image folder

Posted: Thu Oct 10, 2019 11:08 am
by Sendlingur
Thanks Jos for your help
This works but I ended up using another solution which gives the same result.