3

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?>
5
  • What doesn't work? Are you having trouble getting the folder name? dirname($file) Commented Jan 20, 2013 at 22:16
  • yes i dont know how to get the folder name Commented Jan 20, 2013 at 22:22
  • That should be what dirname($file) gets you. Commented Jan 20, 2013 at 22:23
  • i have tried to add $path = dirname($file); and then add $path to the $randomlist .= '<p><a href="../'.$path.'/'.$file.'">'.$title.'</a></p>'; I'm still learning php you see so i dont know. Commented Jan 20, 2013 at 22:30
  • try what I posted below. It isn't actually necessary to do either basename() or dirname(). Commented Jan 20, 2013 at 22:31

1 Answer 1

1

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>';
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.