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 孟德尔随机化结果不一致
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)