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);
?> 
有帮助吗?

解决方案

Why not just modify the line

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

to

if ($file != "." && $file != ".." && $file != 'index.php') {

其他提示

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));
    // ...
}

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');
});

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>';
    }
}
?>
while (false !== ($file = readdir($handle)))
{
    if ($file != "." && $file != ".." && $file != 'index.php')
        $fileTab[] = $file;
}

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

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top