0

The code below will select all of my php files from the named folder and then shuffle them and echo 10 results on my page, the folder contains an index.php file which i would like to be excluded from the results.

<?php 

if ($handle = opendir('../folder/')) {
    $fileTab = array();
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            $fileTab[] = $file;
        }
    }
    closedir($handle);
    shuffle($fileTab);
    foreach(array_slice($fileTab, 0, 10) as $file) {
        $title = str_replace('-', ' ', pathinfo($file, PATHINFO_FILENAME));
        $thelist .= '<p><a href="../folder/'.$file.'">'.$title.'</a></p>';
    }
}
?>
<?=$thelist?>

I have found a code to exclude index.php but I'm not sure how to incorporate it into my code above.

<?php
$random = array_values( preg_grep( '/^((?!index.php).)*$/', glob("../folder/*.php") ) );
$answer = $random[mt_rand(0, count($random) -1)];
include ($answer);
?> 
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Daisy Oopsy
  • 155
  • 9

7 Answers7

2

Why not just modify the line

if ($file != "." && $file != "..") {

to

if ($file != "." && $file != ".." && $file != 'index.php') {
shanethehat
  • 15,460
  • 11
  • 57
  • 87
1

An approach based on glob() instead of readdir():

<?php 
$files = glob('../folder/*.php');
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));
    // ...
}
Niko
  • 26,516
  • 9
  • 93
  • 110
  • I like your way of using glob which i can use to echo random files from multiple folders eg:$files = (glob('../{folder1,folder2,folder3}/*.php', GLOB_BRACE)); could you tell me how do you have the chosen folder echo in the link $thelist .= '

    '.$title.'

    ';
    – Daisy Oopsy Jan 20 '13 at 21:13
1

You can use

$it = new FilesystemIterator(__DIR__, FilesystemIterator::SKIP_DOTS);
$it = new RegexIterator($it, '/.php$/i', RegexIterator::MATCH);
$exclude = array("index.php");
foreach ( $it as $splFileInfo ) {
    if (in_array($splFileInfo->getBasename(), $exclude))
        continue;

    // Do other stuff
}

Or Simply

$files = array_filter(glob(__DIR__ . "/*.php"), function ($v) {
    return false === strpos($v, 'index.php');
});
Baba
  • 94,024
  • 28
  • 166
  • 217
0

You can exclude it while you reading directory content (like you do with '.' and '..'):

if ($handle = opendir('../folder/')) {
    $fileTab = array();
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != ".." && $file != "index.php") {
            $fileTab[] = $file;
        }
    }
    closedir($handle);
    shuffle($fileTab);
    foreach(array_slice($fileTab, 0, 10) as $file) {
        $title = str_replace('-', ' ', pathinfo($file, PATHINFO_FILENAME));
        $thelist .= '<p><a href="../folder/'.$file.'">'.$title.'</a></p>';
    }
}
?>
clover
  • 4,910
  • 1
  • 18
  • 26
0
while (false !== ($file = readdir($handle)))
{
    if ($file != "." && $file != ".." && $file != 'index.php')
        $fileTab[] = $file;
}
Tommaso Belluzzo
  • 23,232
  • 8
  • 74
  • 98
0

you could just change this line if ($file != "." && $file != "..") { to if ($file != "." && $file != ".." && $file != 'index.php') {

WhyteWolf
  • 456
  • 2
  • 9
0

The code you found replaces your cumbersome directory reading loop.

And it should be just:

 $files = preg_grep('~/index\.php$~', glob("../folder/*.php"), PREG_GREP_INVERT);

Get 10 elements as before:

 $files = array_slice($files, 0, 10);

Then output those.

mario
  • 144,265
  • 20
  • 237
  • 291