1
function read_comments() {
if (!is_dir(COMMENT_DIR) || !is_readable(COMMENT_DIR))
    return false;
$file_list = glob(COMMENT_DIR . '*20130721T161046-1262521337.txt');
$comments = array();
foreach ($file_list as $file_name) {
    if (($data = file($file_name)) !== false) {
        $comments[] = unserialize_comment($data);
    }
}
return $comments;}

How would i be able to put a random file name in the glob()? The foreach() keeps giving me trouble.

  • What do you mean by `random file name`? What trouble are you getting? – Mike Brant Jun 11 '14 at 00:16
  • This answer may be of help http://stackoverflow.com/a/4478788/ – Funk Forty Niner Jun 11 '14 at 00:21
  • if (!defined('COMMENT_DIR')) define('COMMENT_DIR', 'comments/'); forgot to include this. I need some kind of function to go into the glob so that it can fetch a random file from the comments/ directory. – user3728070 Jun 11 '14 at 00:22
  • but every time i add a variable inplace of the .txt file it gives me the foreach() error Warning: Invalid argument supplied for foreach() – user3728070 Jun 11 '14 at 00:26
  • I am really just trying to get the name of a random textfile in the comments/ dir and then need printed `$file_list = glob(COMMENT_DIR . '*<--here-->');` – user3728070 Jun 11 '14 at 00:36
  • Show he code that gives you an error, the working code is not very helpful to solve the problem. – jeroen Jun 11 '14 at 00:55
  • `function random_filename($dir = 'comments/') { $files = glob($dir . '/*.txt*'); $file = array_rand($files); return $files[$file]; } if (!defined('COMMENT_DIR')) define('COMMENT_DIR', 'comments/'); function read_comments() { if (!is_dir(COMMENT_DIR) || !is_readable(COMMENT_DIR)) return false; $file_list = glob(COMMENT_DIR . '*random_filename()'); $comments = array(); foreach ($file_list as $file_name) { if (($data = file($file_name)) !== false) { $comments[] = unserialize_comment($data); } } return $comments; }` – user3728070 Jun 11 '14 at 01:05
  • I dont think the foreach is giving me trouble i am sorry. i just dont know how to put in the variable in `$file_list = glob(COMMENT_DIR . '*random_filename()');` where it says random_filename @jeroen – user3728070 Jun 11 '14 at 01:06
  • Have a look at http://www.php.net//manual/en/function.array-rand.php e.g. $random_filename = $file_list[array_rand($file_list)]; – rjdown Jun 11 '14 at 01:12
  • now it gave me `Invalid argument supplied for foreach()` on line `` – user3728070 Jun 11 '14 at 01:19
  • I am doing a horrible job of explaining my problem. My first snippit of code displays one comment but just that specific comment. i am trying to make it display a single random comment on each load. – user3728070 Jun 11 '14 at 01:25
  • so in the 20130721T161046-1262521337.txt place i need a variable of some sort to randomize file names and then place that file name in that spot. Do you see what i mean? – user3728070 Jun 11 '14 at 01:28

1 Answers1

1

This function returns a random file from a given folder. It also allows extension filtering.

function RandomFile($folder='COMMENT_DIR', $extensions='.txt'){

    // fix path:
    $folder = trim($folder);
    $folder = ($folder == '') ? './' : $folder;

    // check folder:
    if (!is_dir($folder)){ die('invalid folder given!'); }

    // create files array
    $files = array();

    // open directory
    if ($dir = @opendir($folder)){

        // go trough all files:
        while($file = readdir($dir)){

            if (!preg_match('/^\.+$/', $file) and 
                preg_match('/\.('.$extensions.')$/', $file)){

                // feed the array:
                $files[] = $file;                
            }            
        }        
        // close directory
        closedir($dir);    
    }
    else {
        die('Could not open the folder "'.$folder.'"');
    }

    if (count($files) == 0){
        die('No files where found :-(');
    }

    // seed random function:
    mt_srand((double)microtime()*1000000);

    // get an random index:
    $rand = mt_rand(0, count($files)-1);

    // check again:
    if (!isset($files[$rand])){
        die('Array index was not found! very strange!');
    }

}

And use

$file_list = glob($folder . $rand_file);
Milad Abooali
  • 686
  • 1
  • 13
  • 30