dsft8327 2013-01-20 22:12
浏览 59
已采纳

php echo文件夹名称使用glob

The code used below will take 10 random files from any of the 3 folders listed in GLOB_BRACE.

eg:

$files = (glob('../{folder1,folder2,folder3}/*.php', GLOB_BRACE)); 

i would like to echo the folder name in the url seen below $thelist

$thelist .= '<p><a href="../'.$folder 1 or 2 or 3.'/'.$file.'">'.$title.'</a></p>';

So when it's displayed on my page it reads.

<p><a href="../folder1/page-name.php">what ever</a></p>
<p><a href="../folder3/page-name.php">what ever</a></p>
<p><a href="../folder1/page-name.php">what ever</a></p>
<p><a href="../folder2/page-name.php">what ever</a></p>
<p><a href="../folder1/page-name.php">what ever</a></p>
<p><a href="../folder3/page-name.php">what ever</a></p>
<p><a href="../folder2/page-name.php">what ever</a></p>
<p><a href="../folder3/page-name.php">what ever</a></p>
<p><a href="../folder1/page-name.php">what ever</a></p>
<p><a href="../folder2/page-name.php">what ever</a></p>

Code used:

<?php 
$files = (glob('../{folder1,folder2,folder3}/*.php', GLOB_BRACE)); /* change php to the file you require either html php jpg png. */
shuffle($files);
$selection = array_slice($files, 0, 11);

foreach ($selection as $file) {
    $file = basename($file);
    if ($file == 'index.php') continue;

    $title = str_replace('-', ' ', pathinfo($file, PATHINFO_FILENAME));
        $randomlist .= '<p><a href="../'.$folder 1 or 2 or 3.'/'.$file.'">'.$title.'</a></p>';
    }
?>
<?=$randomlist?>
  • 写回答

1条回答 默认 最新

  • duanaoshu1989 2013-01-20 22:28
    关注

    The glob() will return the directory and filename. Therefore if you don't reassign $file to basename($file), the entire string will remain intact for output. You can still check basename() in the if() condition to continue.

    foreach ($selection as $file) { 
      // Call basename() in the if condition, but don't reassign the variable $file
      if (basename($file) == 'index.php') continue;
    
      $title = str_replace('-', ' ', pathinfo($file, PATHINFO_FILENAME));
      // Using the full `$file` in the HTML output. No need for basename() or dirname().
      // Using htmlentities to encode the file path for an HTML attribute
      $randomlist .= '<p><a href="' . htmlentities($file, ENT_QUOTES) . '">'.$title.'</a></p>';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于PLC的三轴机械手程序
  • ¥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 海浪数据 南海地区海况数据,波浪数据