SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Mar 2012
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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 Code:
    <?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!!

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    70 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    $directory 
    "some/directory";
    $files scandir($directory);
    $target mt_rand(2,count($files)-1);
    echo 
    "<a href='".$directory.$files[$target]."'>".$files[$target]."</a>";
    ?>
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Member
    Join Date
    Mar 2012
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    70 Post(s)
    Tagged
    0 Thread(s)
    No, it will retrieve all files in the directory.

    PHP Code:
    <?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>";
    ?>
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  5. #5
    Non-Member bronze trophy
    Join Date
    Nov 2009
    Location
    Keene, NH
    Posts
    3,760
    Mentioned
    23 Post(s)
    Tagged
    0 Thread(s)
    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.

    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.

  6. #6
    SitePoint Member
    Join Date
    Mar 2012
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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!!!

  7. #7
    Non-Member bronze trophy
    Join Date
    Nov 2009
    Location
    Keene, NH
    Posts
    3,760
    Mentioned
    23 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:


    Quote Originally Posted by CathEtc View Post
    We have a winner!
    Not sure why you switched it back to the slower crappy bad coding methodology on the echo though
    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?

  8. #8
    SitePoint Member
    Join Date
    Mar 2012
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by deathshadow60 View Post
    WHY do people do this?!?
    Sorry 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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •