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:
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.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 am grateful for ideas. Thanks!!


Reply With Quote


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