Random Link Help

Hey all,
So I’m trying to create a button that will open a random .php file from a given directory. I’m relatively new to .php (like, I’ve been diving in for the past 3 weeks), so I don’t understand a lot of the nitty-gritty stuff. But I’ve been able to tweak some code (originally for displaying random images) and get it to almost do what I want:

<?php
  $image_path = "my/directory";
  $types = array(
    "PHP" => "php");
  $file_list = array();
 $image_path = realpath(trim($image_path) != "" ? $image_path : ".") . "/";
  if ($image_path !== false && is_dir($image_path) && $handle = @opendir($image_path)) {
    while (($file = readdir($handle)) !== false) {
      if (is_file($image_path . $file)) {
        $file_extension = strtoupper(substr(strrchr($file, "."), 1));
        if (isset($types[$file_extension])) {
          $file_list[] = array(
            "filename" => $image_path . $file,
            "mimetype" => $types[$file_extension]); } } } }
  if (sizeof($file_list) > 0) {
    shuffle($file_list);
    @readfile($file_list[0]["filename"]); }
  else { die("No files found."); }
?>

I’m getting a random file, but it’s displaying the whole page instead of a link. I know i need to use echo and <a href> in there at the end, but I don’t know how. I tried about a dozen different variations on $file_list, $file, “filename”, etc., but nothing worked.

I am grateful for ideas. Thanks!!

<?php
$directory = "some/directory";
$files = scandir($directory);
$target = mt_rand(2,count($files)-1);
echo "<a href='".$directory.$files[$target]."'>".$files[$target]."</a>";
?>

Thanks, StarLion. I should have mentioned that I have files with several extension in the directory, but I only need .php files, and not any other types. Will this code do that?

No, it will retrieve all files in the directory.


<?php
$directory = "some/directory";
$files = scandir($directory);
$files = preg_grep("~.*\\.php$~",$files);
$target = mt_rand(0,count($files)-1);
echo "<a href='".$directory.$files[$target]."'>".$files[$target]."</a>";
?> 

I’d suggest using GLOB instead of scandir plus a regex – since it lets you do wildcards so you’re not using as much memory thrashing and giving the garbage collector rikki fits. Also helps to put the trailing slash on directory, so when you composite them in the echo you don’t need more code.


<?php
$directory='some/directory/';
$files=glob($directory.'*.php');
$target=mt_rand(0,count($files)-1);
echo '
	<a href="',$directory,$files[$target],'">
		',$files[$target],'
	</a>';
?>
Off Topic:

… and of course being echo one shouldn’t be using double quotes or string addition. :smiley:

We have a winner!

<?php
$directory=‘./my/directory/’;
$files=glob($directory.‘*.php’);
$target=mt_rand(0,count($files)-1);
echo “<a href='”.$files[$target].“'>RANDOM</a>”;
?>

Thanks to you both!!!

Off Topic:

Not sure why you switched it back to the slower crappy bad coding methodology on the echo though :smiley:
Double quotes and string addition on echo… WHY do people do this?!? Just TRYING to give the garbage collector fits and make the code harder to follow AND run slower?

Sorry :frowning: In my noob-ness (3 weeks of PHP), i really don’t know better. Your original example was repeated the directory twice in the results (…/my/dir/my/dir/ instead of …/my/dir/), so I used StarLion’s echo function, which WAS displaying correct paths.