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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀