1

我有一个 php 脚本,它从一个目录中抓取 10 个随机图像并将它们显示在一个页面上。我通过名为 rand_boats.php 的包含将此脚本包含在自定义 wordpress 模板页面中。我不断触发给出无效文件夹的错误消息。rand_boats 和boats 目录都在同一个目录中。这是脚本:

<?php function RandomFile($folder='', $extensions='.*'){
   // 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!');
    }

    // return the random file:
    return $folder . "/" . $files[$rand];

}


$random1 = RandomFile("img");
while (!$random2 || $random2 == $random1) {
    $random2 = RandomFile("img");
}
while (!$random3 || $random3 == $random1 || $random3 == $random2) {
    $random3 = RandomFile("img");
}
while (!$random4 || $random4 == $random1 || $random4 == $random2 || $random4 == $random3) {
    $random4 = RandomFile("img");
}
while (!$random5 || $random5 == $random1 || $random5 == $random2 || $random5 == $random3 || $random5 == $random4) {
    $random5 = RandomFile("img");
}
while (!$random6 || $random6 == $random1 || $random6 == $random2 || $random6 == $random3 || $random6 == $random4 || $random6 == $random5) {
    $random6 = RandomFile("img");
}
while (!$random7 || $random7 == $random1 || $random7 == $random2 || $random7 == $random3 || $random7 == $random4 || $random7 == $random5 || $random7 == $random6) {
    $random7 = RandomFile("img");
}
while (!$random8 || $random8 == $random1 || $random8 == $random2 || $random8 == $random3 || $random8 == $random5 || $random8 == $random5 || $random8 == $random6 || $random8 == $random7) {
    $random8 = RandomFile("img");
}
while (!$random8 || $random9 == $random1 || $random9 == $random2 || $random9 == $random3 || $random9 == $random5 || $random9 == $random5 || $random9 == $random6 || $random9 == $random7 || $random9 == $random8) {
    $random9 = RandomFile("img");
}
while (!$random8 || $rand10 == $random1 || $random10 == $random2 || $random10 == $random3 || $random10 == $random5 || $random10 == $random5 || $random10 == $random6 || $random10 == $random7 || $random10 == $random87 || $random10 == $random9) {
    $random10 = RandomFile("img");
}
?>

<div id="random_images">
<ul>
    <li><img src="<?php bloginfo('template_directory'); ?>/<?php echo $random1; ?>" alt="image alt" /></li>
    <li><img src="<?php bloginfo('template_directory'); ?>/<?php echo $random2; ?>" alt="image alt" /></li>
    <li><img src="<?php bloginfo('template_directory'); ?>/<?php echo $random3; ?>" alt="image alt" /></li>
    <li><img src="<?php bloginfo('template_directory'); ?>/<?php echo $random4; ?>" alt="image alt" /></li>
    <li><img src="<?php bloginfo('template_directory'); ?>/<?php echo $random5; ?>" alt="image alt" /></li>
    <li><img src="<?php bloginfo('template_directory'); ?>/<?php echo $random6; ?>" alt="image alt" /></li>
    <li><img src="<?php bloginfo('template_directory'); ?>/<?php echo $random7; ?>" alt="image alt" /></li>
    <li><img src="<?php bloginfo('template_directory'); ?>/<?php echo $random8; ?>" alt="image alt" /></li>
    <li><img src="<?php bloginfo('template_directory'); ?>/<?php echo $random9; ?>" alt="image alt" /></li>
    <li><img src="<?php bloginfo('template_directory'); ?>/<?php echo $random10; ?>" alt="image alt" /></li>
</ul>
   </div>
4

1 回答 1

0

如果你在检查它是否是一个目录之前打印出 $folder 的值,你会得到什么?

于 2012-05-04T19:54:12.087 回答