Using opendir with PHP Pages Component for Joomla 1.5

330 views Asked by At

I have a website running on Joomla! 1.5 (can't upgrade because of template issue).
On this site, I have a page where members can find some important files, needed for the membership in our club.

I arranged these files in some folders, all using the same naming convention ( *yyyy-mm-dd_documentName* ).

To make things easier, I want to automaticly list the files, using some formatting.

As Joomla doesn't allow PHP to be entered in articles, I installed the PHP Pages Component.
This component allows you to create pages and link to them via e.g. the menu bar.

Too list those files, I want to use the PHP's opendir function, opening the folder and run over the folder to read the file names and format them.
This is the code I was using for that:

if($handle = opendir('/media/files/')) {
  while(false !== ($entry = readdir($handle))) {
    echo $entry;
  }

  closedir($handle);
}

This gave an error as the component starts every relative path from it's own dir, ignoring the first / for some reason.
So I tried to fix that by entering the full URL (starting from http://).

Still not fixed, the new error now is (bestanden.php is the PHP page)

Warning: opendir(http://www.example.com/media/files) [function.opendir]: failed to open dir: not implemented in /home/user/public_html/components/com_php/files/bestanden.php on line 38

Someone advised me to use the ftp-handlet (which is also supported).
The syntax for this apperantly is ftp://user:[email protected]
I made a seperate account for this, ending up immediately at the right dir, preventing cross-dir attacks. This does work if I manually try this in my browser (except that it still asks for credentials).
But this didn't work either.

I'm running out of ideas.
Can anyone help me?

SOLUTION

Thanks to @Lodder

<ul>
<?php

  $path = JPATH_SITE . '/media/files/nieuwsbrieven/'; 
  $files = JFolder::files($path);  
  foreach ($files as $file) {
    // Strip extension and save name to display it
    $name = substr($file, 0, strrpos($file, '.'));
    // Exclude the index I placed inside the dir to prevent direct surfing to the folder
    if($name != "index") {
      echo "<li><a href='/media/files/nieuwsbrieven/" . $file . "' title='Nieuwsbrief " . $name . "' target='_blank'>Nieuwsbrief " . $name . "</a></li>";
    }
  }

?>
</ul>

Result

  • Nieuwsbrief 2013-04-03
  • Nieuwsbrief 2013-04-19
  • Nieuwsbrief 2013-05-16
  • Nieuwsbrief 2013-06-19
  • Nieuwsbrief 2013-07-17
  • Nieuwsbrief 2013-08-28
1

There are 1 answers

8
Lodder On BEST ANSWER

You should really looks at the Joomla Docs when trying things out. Joomla has it's own class for reading files within a folder. You ca use the following to display all files within your folder:

$path = JPATH_SITE . '/media/files';
$files = JFolder::files($path);

foreach ($files as $file) {
    echo $file;
}

To use this code, forget the likes of components allowing you to use custom PHP etc, I would highly recommend a plugin called Sourcerer which allows the use of PHP or JS in articles.

For more information on the Joomla filesystem classes, have a read of this:

http://docs.joomla.org/How_to_use_the_filesystem_package#Read_files_from_folder

Hope this helps