0

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:password@ftp.example.com
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
BlueCacti
  • 9,729
  • 3
  • 20
  • 24

1 Answers1

1

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

Lodder
  • 19,758
  • 10
  • 59
  • 100
  • JPATH_SITE is a global variabel I guess? Does it lead to my root folder or some specific folder? – BlueCacti Dec 11 '13 at 13:17
  • `JPATH_SITE` leads you to the root of your Joomla site. Test the code and have a look ;) – Lodder Dec 11 '13 at 13:22
  • Was my first guess. But I'm unable to test it right now, will be able to start working on this in a few hours. Thanks for the help! – BlueCacti Dec 11 '13 at 13:39
  • Is there a J!1.5 version of that extension? Or some similar extension? Anyway, I need to be able to use PHP for this – BlueCacti Dec 15 '13 at 00:12
  • This is simply code that will do exactly what you need it to do. It will work with Joomla 1.5 – Lodder Dec 15 '13 at 00:24
  • I'll play around with the snipper you gave and I'll mark this as an answer once I get it to work – BlueCacti Dec 15 '13 at 17:48
  • Fantastic! It works :) I'll edit my answer to include the final code. Thanks again! – BlueCacti Dec 16 '13 at 19:22
  • 1
    You're more than welcome, glad it's all working. Please would you also mind committing to the [Joomla Answers Proposal](http://area51.stackexchange.com/proposals/58842/joomla). This would be much appreciated as we really want to get this approved on StackExchange. – Lodder Dec 16 '13 at 19:26