简体   繁体   English

将opendir与Joomla 1.5的PHP Pages组件一起使用

[英]Using opendir with PHP Pages Component for Joomla 1.5

I have a website running on Joomla! 我有一个在Joomla上运行的网站! 1.5 (can't upgrade because of template issue). 1.5(由于模板问题无法升级)。
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* ). 我将这些文件排列在一些文件夹中,所有文件都使用相同的命名约定(* 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 . 由于Joomla不允许在文章中输入PHP,因此我安装了PHP Pages Component
This component allows you to create pages and link to them via eg 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. 要列出这些文件,我想使用PHP的opendir函数,打开该文件夹并在该文件夹上运行以读取文件名并设置其格式。
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://). 因此,我尝试通过输入完整的URL(从http://开始)来解决此问题。

Still not fixed, the new error now is (bestanden.php is the PHP page) 仍然没有解决,现在的新错误是(bestanden.php是PHP页面)

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 警告: opendir( http://www.example.com/media/files )[function.opendir]:无法打开dir:在/home/user/public_html/components/com_php/files/bestanden.php中未实现38

Someone advised me to use the ftp-handlet (which is also supported). 有人建议我使用ftp-handlet(也受支持)。
The syntax for this apperantly is ftp://user:password@ftp.example.com 相应地,语法为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 感谢@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-03
  • Nieuwsbrief 2013-04-19 Nieuwsbrief 2013-04-19
  • Nieuwsbrief 2013-05-16 Nieuwsbrief 2013年5月16日
  • Nieuwsbrief 2013-06-19 Nieuwsbrief 2013年6月19日
  • Nieuwsbrief 2013-07-17 Nieuwsbrief 2013-07-17
  • Nieuwsbrief 2013-08-28 Nieuwsbrief 2013-08-28

You should really looks at the Joomla Docs when trying things out. 试用时,您应该真正查看Joomla文档。 Joomla has it's own class for reading files within a folder. Joomla拥有自己的类,用于读取文件夹中的文件。 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. 要使用此代码,请忘记允许您使用自定义PHP的组件之类的东西,我强烈建议您使用名为Sourcerer的插件,该插件允许在文章中使用PHP或JS。

For more information on the Joomla filesystem classes, have a read of this: 有关Joomla文件系统类的更多信息,请阅读以下内容:

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

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM