dtnwm4807 2013-12-11 12:45
浏览 51
已采纳

使用opendir和PHP页面组件为Joomla 1.5

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
  • 写回答

1条回答 默认 最新

  • douyi1197 2013-12-11 12:53
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答